Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Created May 25, 2013 15:58
Show Gist options
  • Save PatrickJS/5649556 to your computer and use it in GitHub Desktop.
Save PatrickJS/5649556 to your computer and use it in GitHub Desktop.
Simple calculator make in one ruby function
def calculator(expression, *args)
{
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 }
}[expression].call(*args)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment