Forum: Ruby create a string from a pattern?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul R. (Guest)
on 2007-07-11 17:29
(Received via mailing list)
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
Robert K. (Guest)
on 2007-07-11 17:33
(Received via mailing list)
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
Shawn S. (Guest)
on 2007-07-11 18:19
(Received via mailing list)
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:in `require'
        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:in `require'
        from ./test.rb:4


Any help would be appreciated.
Paul R. (Guest)
on 2007-09-26 01:04
(Received via mailing list)
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 ;-)
I had to do this ( just in case any one else tries it )
require 'enumerator'

Thanks!
Paul
This topic is locked and can not be replied to.