From: Brian Adkins Date: 2007-11-28T10:45:01+09:00 Subject: Re: Most elegant way to do this? On Nov 27, 5:50 pm, William James wrote: > On Nov 27, 7:17 am, Brian Adkins wrote: > > > > > On Nov 27, 6:26 am, William James wrote: > > > > On Nov 26, 6:17 pm, rbysam...@gmail.com wrote: > > > > > Are there any more elegant, concise, pithy, and more Rubyish ways of > > > > doing this? > > > > > def roll(number_of_dice) > > > > sum = 0 > > > > number_of_dice.times do > > > > sum += rand(5).next > > > > end > > > > sum > > > > end > > > > > Thanks in advance! > > > > def sum *x > > > x.inject{|a,b| a+b} > > > end > > > def roll n > > > sum( *(1..n).map{rand(6)+1} ) > > > end > > > Clever, but 3 times slower than the original (after correcting it). > > What so-called correction did you imagine that you had to make? Sorry for the ambiguity; "it" referred to the original - see other posts for the imagined correction, so called ;)