Skip to content

Instantly share code, notes, and snippets.

@vsvld
Created January 17, 2016 21:38
Show Gist options
  • Save vsvld/1bcfe3f831d14c170117 to your computer and use it in GitHub Desktop.
Save vsvld/1bcfe3f831d14c170117 to your computer and use it in GitHub Desktop.
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