Randexp - Generating test data with Ruby regexs

While paging through reddit programming recently (seems only fair since they linked to me ;)) I stumbled on to the very nifty Randexp gem, a library that uses regular expression patterns to generate data that would satisfy the pattern. Or in less tech terms - a really good test data generator.

# install randexp

$ irb

require "rubygems"
require "randexp"

# simple fake phone number -
/020(7|8) \d{3} \d{4}/.gen

# build a reusable class.
class Randgen
  def self.version()
   /\d{1,3}\.\d{1,4}/.gen
  end
end

# and use it.
/[:version:]/.gen

I especially like the ability to make your own character classes. I’m not a ruby guy but I can see this being very useful in lots of little data generation scripts and test harnesses.