Skip to content

Instantly share code, notes, and snippets.

@seaneshbaugh
Last active August 26, 2020 15:36
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 seaneshbaugh/c7df4751fd437f66f229 to your computer and use it in GitHub Desktop.
Save seaneshbaugh/c7df4751fd437f66f229 to your computer and use it in GitHub Desktop.
dice
class Integer
DICE_REGEX = /\Ad(\d+)\z/.freeze
def method_missing(method_name, *args, &block)
sides = method_name.to_s.match(DICE_REGEX)
super unless sides
times.map { rand(sides[1].to_i) + 1 }.inject(:+)
end
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.match(DICE_REGEX) || super
end
end
puts 1.d10
puts 2.d20
puts 3.d6
puts 4.respond_to?(:d12)
puts 4.respond_to?(:dsgfhf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment