Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.