Created
January 17, 2016 21:38
-
-
Save vsvld/1bcfe3f831d14c170117 to your computer and use it in GitHub Desktop.
This file contains 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
class CalculatorController < ApplicationController | |
before_action :set_first_number | |
before_action :set_second_number, except: :square_root | |
def addition | |
result = @first_number + @second_number | |
render json: result | |
end | |
def subtraction | |
result = @first_number - @second_number | |
render json: result | |
end | |
def multiplication | |
result = @first_number * @second_number | |
render json: result | |
end | |
def division | |
result = @first_number / @second_number | |
render json: result | |
end | |
def square_root | |
render json: (@first_number.present? && @first_number > 0) ? @first_number**0.5 : 'must be > 0' | |
end | |
private | |
def set_first_number | |
@first_number = params[:n1].to_f if params[:n1].present? | |
end | |
def set_second_number | |
@second_number = params[:n2].to_f if params[:n2].present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment