tags:

views:

263

answers:

3

I need to index into a hash that I have defined in terms of "true" and "false"

colorHash = Hash.new { |hash, key| hash[key] = {} }
colorHash["answers"][true]  = "#00CC00"
colorHash["answers"][false] = "#FFFFFF"

For testing purposes, I am indexing with rand(2) and that fails. If I index with true it works.

I was looking for something like

rand(2).logical

but find nothing.

+11  A: 

There is a simple (although not very exciting) way to do this:

rand(2) == 1
Mark Byers
+1  A: 

[true,false].shuffle or [true,false].sort { rand }

def get_bool
   [true,false].shuffle.shift
end
ezpz
+1  A: 

How about something like this?

[true,false][rand(2)]

That is, return a random result from the true/false array. It's certainly more verbose than rand(2) == 1, though.

jerhinesmith