B
Ben Stephens
I was reading Learn to Program by Chris Pine yesterday, in one bit it says:
=09' Note that I used rand(101) to get back numbers from 0 to 100, and
that rand(1) always gives back 0.'
I was wondering if rand() could support syntax like:
rand(0..100)
Here's my go:
def random(value)
=09if value.class.method_defined? 'entries'
=09=09entries =3D value.entries
=09=09entries[rand(entries.length)]
=09else
=09=09rand(value)
=09end
end
this works with arrays and hashes too.
I'm coming to Ruby via RoR and i'm loving it, I should be writing a
web app and i'm playing with Ruby instead.
Ben
=09' Note that I used rand(101) to get back numbers from 0 to 100, and
that rand(1) always gives back 0.'
I was wondering if rand() could support syntax like:
rand(0..100)
Here's my go:
def random(value)
=09if value.class.method_defined? 'entries'
=09=09entries =3D value.entries
=09=09entries[rand(entries.length)]
=09else
=09=09rand(value)
=09end
end
this works with arrays and hashes too.
I'm coming to Ruby via RoR and i'm loving it, I should be writing a
web app and i'm playing with Ruby instead.
Ben