Skip to content

Instantly share code, notes, and snippets.

@sinisterchipmunk
Created January 16, 2012 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinisterchipmunk/1618498 to your computer and use it in GitHub Desktop.
Save sinisterchipmunk/1618498 to your computer and use it in GitHub Desktop.
Code examples for "Welcome to my world!" - http://localhost:3000/mist/posts/welcome-to-my-world
class Dice
def initialize(count, sides)
@count, @sides = count, sides
end
def roll
@count.times.inject(0) { |a,i| a + rand(sides) }
end
end
class Fixnum
[1, 2, 4, 6, 8, 10, 12, 20].each do |sides|
define_method :"d#{sides}" do
Dice.new(self, sides).roll
end
end
end
2.d4 #=> rand(4) + rand(4)
3.d6 #=> rand(6) + rand(6) + rand(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment