On Tue, May 10, 2011 at 10:56 PM, Kevin <darkintent / gmail.com> wrote:
> I'm trying to generate a random set of strings to fill a database with th=
at
> match the following pattern: =A0/^([A-Z]|[a-z]|-)+$/
>
> The array I'm trying to create must contain strings that look like
> "A12-34-4567" and be exactly ten characters long I have seen code on the

That's 11 characters btw.

> internet for generating random strings but I don't have internet at home =
at
> the moment to really study examples like the one here:
> http://coderrr.wordpress.com/2008/10/28/ruby-golf-generating-random-strin=
gs

Just a hint: that does not work on 1.9 because ?a does not return a
Fixnum any more.

> I'm guessing that something like map { if =A0/^([A-Z]|[a-z]|-)+$/ =A0&
> char.len=3D=3D10
> =A0strings<< current_matching_value
> }
>
> would achieve the right solution. =A0Any pointers would be greatly
> appreciated.

Often there is not _the_ right solution (-> TIMTOWTDI).  So far nobody
seems to have suggested good old sprintf:

BASE =3D 'A'.getbyte 0
DELTA =3D 'Z'.getbyte(0) - BASE

10.times do
  s =3D sprintf '%s%02d-%02d-%04d',
    (BASE + rand(DELTA)).chr,
    rand(100),
    rand(100),
    rand(10000)

  puts s, s.length
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/