Phrozen and many others are using Hackety Hack to learn how to code.

Hackety Hack is a free program for Windows, Mac OS X and Linux that can teach you how to make games, applications and more and share those programs with your friends. You can ask other budding programmers questions and follow them to see what code they're working on through the Hackety Hack website.

Sign up Learn More

dicer by Phrozen

class Dice
  attr_reader :faces, :rolls, :result
  
  def initialize(faces)
    @faces = faces
    @rolls = []
  end
  
  def roll(n=1)
    self.reset!
    n.times { @rolls << 1 + rand(@faces)) }
    @result = @rolls.inject(:+)
  end
  
  def reset!
    @rolls.clear
    @result = nil
  end
end

dice = new Dice(ask("How many faces?").to_i)
dice.roll(ask("How many times? [default=1]").to_i)
alert("You rolled #{rolls} for a total of: #{dice.result}")