Ben Stephens <foolfodder / gmail.com> writes:

> I was wondering if rand() could support syntax like:
>
>    rand(0..100)

That's a great idea!

> Here's my go:
>
> def random(value)
> 	if value.class.method_defined? 'entries'
> 		entries = value.entries
> 		entries[rand(entries.length)]
> 	else
> 		rand(value)
> 	end
> end
>
> this works with arrays and hashes too.

Oh, I don't know about that...  I'd do something like this:

 def random(range)
   case range
   when Integer
     rand(range)
   when Range
     a, b = [range.begin, range.end].sort
     if a.integer? and b.integer? then
       if range.exclude_end?
       then rand(b - a) + a
       else rand(b - a + 1) + a end
     else
       rand * (b - a) + a
     end
   end
 end

> 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.

That's the downside of using such a powerful language:
You can never stop playing with it! :-)

-- 
Daniel Brockman <daniel / brockman.se>

    So really, we all have to ask ourselves:
    Am I waiting for RMS to do this?   --TTN.