Skip to content

Instantly share code, notes, and snippets.

@toddlers
Created June 2, 2013 07:12
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 toddlers/5692889 to your computer and use it in GitHub Desktop.
Save toddlers/5692889 to your computer and use it in GitHub Desktop.
simple ruby calculator
class Calculator
def initialize
@cal={
add: ->(a,b) { a + b },
subtract: ->(a,b) { a - b },
divide: ->(a,b) { a / b },
multiply: ->(a,b) { a * b },
power: ->(a,b) { a ** b }
}
end
def method_missing(expression, a, b)
@cal[expression].call(a,b)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment