Created
January 15, 2014 23:35
-
-
Save nathantross/8446911 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' | |
| 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