Created
January 16, 2012 01:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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