Skip to content

Instantly share code, notes, and snippets.

@nathantross
Created January 15, 2014 23:35
Show Gist options
  • Select an option

  • Save nathantross/8446911 to your computer and use it in GitHub Desktop.

Select an option

Save nathantross/8446911 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'sinatra/reloader'
get "/:operator/*" do
operator = params[:operator].to_s
n = params[:splat][0].split('/')
n.map!{ |e| e.to_f }
case operator
when "add"
n = n.inject{ |sum, n| sum + n }
when "subtract"
n = n.inject(0){ |sum, n| sum - n }
when "multiply"
n = n.inject(1){ |sum, n| sum * n }
when "divide"
for i in (1...n.length)
n = "Cannot divide by zero!" if n[i] == 0
end
n = n.inject{ |sum, n| sum / n }
end
return "#{n}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment