On Nov 26, 5:24 pm, Alex Young <a... / blackkettle.org> wrote:
> 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
>
> The Incredible Inevitable Inject:
>
> def roll(number_of_dice)
>    (0...number_of_dice).inject(0){|m,r| rand(5)+m}
> end

You missed the +1 needed to take rand(5) to 1..6 instead of 0..5.

And I personally like 1..num_dice instead of 0...num_dice. And,
finally, I sum things so often I usually have this lying around:

  module Enumerable
    def sum
      if block_given?
        inject(0){ |sum,obj| sum + yield(obj) }
      else
        inject(0){ |sum,obj| sum+obj }
      end
    end
  end

which makes the solution simply:
  def roll(number_of_dice)
    (1..number_of_dice).sum{ rand(5)+1 }
  end


In the vein of DRY code and unix tools, I strongly encourage everyone
to be on constant vigil looking for bits of code that can be
abstracted out to little atomic re-usable bits. After a while, coding
is less like carving entire models from styrofoam, and more like
snapping little Lego blocks together.