From: Daniel Schierbeck Date: 2006-07-28T02:45:06+09:00 Subject: Re: rand(-10..10) ChrisH wrote: > Daniel Schierbeck wrote: > ... >> This should work with all enumerables: >> >> module Enumerable >> def rand >> entries = entries >> entries.at(Kernel.rand(entries.length)) >> end >> end >> >> I can't seem to get around the `entries = entries' part -- I don't want >> to convert the enumerable to an array more than once. Any ideas? > > Enumerable supports sort_by, so could just use: > > module Enumerable > def rand > self.sort_by{Kernel.rand}.last > end > end I think sorting the entire enumerable may be slight overkill, although it of course is prettier :) Daniel