Alex Young
3/23/2007 1:48:00 PM
D. Krmpotic wrote:
> Hi!
>
> suppose I have this hash:
>
> data = { "a" => 1, "b" => 2, "c" => 3 }
>
> I'd like to get a random pair from the hash.
>
> so
>
> data.get_pair(data.rand)
>
> should return ["b", 2] for example.
>
> How could this be done? I cannot find any method in the documentation.
class Hash
def get_rand_pair
key = self.keys[rand(self.length)]
[key, self[key]]
end
end
--
Alex