Created
July 25, 2019 07:00
-
-
Save harsh183/8c5b6ca8e63dc693a4a763bb0f44d327 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'sinatra' | |
# Ex. http://localhost:4567/calc/add?x=1&y=2 | |
get '/calc/:operation' do | |
operations = { 'add' => ->(x, y) {x + y}, | |
'sub' => ->(x, y) {x - y}, | |
'mul' => ->(x, y) {x * y}, | |
'div' => ->(x, y) {x / y}, | |
'mod' => ->(x, y) {x % y} } | |
# Yes I could DRY that further but I don't trust user input | |
operation = params['operation'] | |
x, y = params['x'].to_f, params['y'].to_f | |
result = operations[operation].call(x, y) | |
result.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MIT License