How about:
def allowed_chars( char_classes = "ul" )
output = []
char_classes.split( // ).each do |curr|
case curr
when "u"
output += ( 'A'..'Z' ).to_a
when "l"
output += ( 'a'..'z' ).to_a
when "d"
output += ( '0'..'9' ).to_a
else
raise ArgumentError, "Invalid value passed to allowed_chars: #{ curr }"
end
end
output
end
... the rest should be fine ;)
j.
Added a few tweaks, hope that is ok 8-)
def allowed_chars( val )
output = []
case val
when 'u' then output += ('A'..'Z').to_a
when 'l' then output += ('a'..'z').to_a
when 'd' then output += ('0'..'9').to_a
when 'uld'
output += ('A'..'Z').to_a
output += ('a'..'z').to_a
output += ('0'..'9').to_a
else
output += ('A'..'Z').to_a
output += ('a'..'z').to_a
end
end
# 'u' - upper, 'l' - lower, 'd' - digit, 'uld' - alpha numeric
# default upper and lower case string
def random_text( length, val='ul' )
v = val
output = ""
allowed = allowed_chars( val )
length.times do
output << allowed[ rand( allowed.length ) ]
end
output
end
--Mark
From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Jeff Wood
Sent: Tuesday, October 04, 2005 1:29 PM
To: [email protected]
Subject: Re: [Wtr-general] OT: generate random text?
I recently posted this, but I'll pop another copy here.
def allowed_chars
output = []
output += ('A'..'Z').to_a
output += ('0'..'9').to_a
end
def random_text( length )
output = ""
allowed = allowed_chars()
length.times do
output << allowed[ rand( allowed.length ) ]
end
output
end
all done with functionality to easily add/remove chars from your set of allowed.
hope that helps.
j.On 10/4/05, Winston, Phyllis H <[EMAIL PROTECTED]> wrote:
Alan Richardson has an excel spreadsheet using visual basic macros for
data generation:
http://www.compendiumdev.co.uk/testdata/index.php
-----Original Message-----
From: Chris McMahon [mailto:[EMAIL PROTECTED]]
Sent: Monday, October 03, 2005 6:33 PM
To: [email protected]
Subject: [Wtr-general] OT: generate random text?
Hi...
I want to generate some random text for test purposes. "rand" doesn't
seem to DWIM. Say, to pick an arbitrary letter from the uppercase
range A to Z.
[A-Z].rand
[A..Z].rand
seems right, but isn't.
-Chris
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general
--
" http://ruby-lang.org -- do you ruby?"
Jeff Wood
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general
--
"http://ruby-lang.org -- do you ruby?"
Jeff Wood
_______________________________________________ Wtr-general mailing list [email protected] http://rubyforge.org/mailman/listinfo/wtr-general
