dot rand

I was just installing faker and stumbled on the delicious little extension that it makes to Array. Namely, you can call array.rand, to select a random element from it.

This is really sweet. So instead of the clunky expression like

a = ['foo', 'bar', 'baz', 'boozer', 'frell', 'dren']
pick = a[rand(a.length)]

...You just call

a.rand

...It also works with ranges, though you have to splat them first (or call to_i):

[*1..20].rand

...Maybe I'm a weenie for thinking so, but I really, really like this! : )

(Note to shelf: Blogger needs a syntax highlighter!)

No comments: