Create a string from a pattern?


#1

Im trying to come up with an easy way to generate a random string,
based on a supplied pattern, maybe something like:

String.from_pattern("\d\d\d@\w{3,5}.com") => “removed_email_address@domain.invalid”

Ive used a standard reg exp format here, but it doesnt matter if it
uses something else.
Any simple way to do this?

Thanks
Paul


#2

On 09.07.2007 17:41, Paul R. wrote:

Im trying to come up with an easy way to generate a random string,
based on a supplied pattern, maybe something like:

String.from_pattern("\d\d\d@\w{3,5}.com") => “removed_email_address@domain.invalid”

Ive used a standard reg exp format here, but it doesnt matter if it
uses something else.
Any simple way to do this?

Dunno whether you’d count this as simple:

“%d-%d{3}-%w{3,5}”.gsub /%(.)(?:{(\d+)(?:,(\d+))?})?/ do |m|
t, mi, ma = $1, $2, $3

rep = case
when mi.nil?
1
when ma.nil?
mi.to_i
else
mi.to_i + rand(1 + ma.to_i - mi.to_i)
end

case t
when “d”
rep.to_enum(:times).inject("") {|s,| s << (?0 + rand(10)).chr}
when “w”
rep.to_enum(:times).inject("") {|s,| s << (?a + rand(1 + ?z -
?a)).chr}
else
# ignore
m
end
end

Kind regards

robert


#3

On Jul 9, 10:25 am, Robert K. removed_email_address@domain.invalid wrote:

 else
   # ignore
   m

end
end

Kind regards

    robert

its more elegant than I had, not sure about simpler :wink:
I had to do this ( just in case any one else tries it )
require ‘enumerator’

Thanks!
Paul


#4

am fairly new to ruby so please help me. I am getting the
“gem_orginal_require” error.
Here is what I have.
The following gems installed:

*** LOCAL GEMS ***
activerecord (1.15.3)
Implements the ActiveRecord pattern for ORM.
activesupport (1.4.2)
Support and utility classes used by the Rails framework.
fastthread (1.0)
Optimized replacement for thread.rb primitives
hoe (1.2.1)
Hoe is a way to write Rakefiles much easier and cleaner.
hpricot (0.6)
a swift, liberal HTML parser with a fantastic library
mechanize (0.6.9)
Mechanize provides automated web-browsing
rake (0.7.3)
Ruby based make-like utility.
rubyforge (0.4.2)
A simplistic script which automates a limited set of rubyforge
operations
scrubyt (0.3.0)
A powerful Web-scraping framework
sources (0.0.1)
This package provides download sources for remote gem installatio

The version of ruby installed: ruby 1.8.6 (2007-03-13 patchlevel 0)
[i686-linux]

And here is the script I am trying to run

#!/usr/bin/ruby

require ‘rubygems’
require ‘scrubyt’

google_data = Scrubyt::Extractor.define do
fetch ‘http://www.google.com/ncr
fill_textfield ‘q’, ‘ruby’
submit

result ‘Ruby P.ming Language’
end

google_data.to_xml.write($stdout, 1)
Scrubyt::ResultDumper.print_statistics(google_data)

And finally the errors I get:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require': no such file to load -- parse_tree_reloaded (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
from /usr/lib/ruby/gems/1.8/gems/scrubyt-0.3.0/lib/scrubyt.rb:9
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in
gem_original_require' from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:inrequire’
from ./test.rb:4

Any help would be appreciated.