From: Phrogz Date: 2007-11-27T23:10:00+09:00 Subject: Re: Most elegant way to do this? On Nov 27, 2:06 am, Robert Klemme wrote: > 2007/11/27, Phrogz : > > > You missed the +1 needed to take rand(5) to 1..6 instead of 0..5. > > And everybody apparently missed 6 because rand(5) will yield *5* > values ranging in 0..4. :-)) Oh crimeny...oops. :p > There is another one, that - at least theoretically - saves some > addition efforts: > > require 'enumerator' > > def roll(number) > raise ArgumentError, "Negative!" if number < 0 > number.to_enum(:times).inject(number) {|s,| s + rand(6)} > end You know, I _want_ to embrace to_enum, because it's certainly excellently powerful and abstract...but somehow I just don't grok it. Thanks for the reminder. Sometime I'll have to think about it. (I wish I could find some flaw with it, and say "no no no, what it really should be is ____"; I just somehow basically find it confusing.)