help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
ruby-calculator
/
db
/
migrate
RSK World
ruby-calculator
Ruby Calculator Pro - Interactive Calculator with 8 Types + Mathematical Functions + Rails MVC + Modern Web Interface + API Integration + Educational Design
migrate
  • 001_create_calculation_histories.rb719 B
  • 002_create_favorite_calculations.rb915 B
calculator_controller.rb
app/controllers/calculator_controller.rb
Raw Download
Find: Go to:
# Ruby Calculator - Interactive calculator built with Ruby on Rails
# Author: RSK World (Molla Samser, Founder | Rima Khatun, Designer & Tester)
# Contact: help@rskworld.in | +91 93305 39277
# Website: https://rskworld.in
# Year: 2026
# Description: Build a fully functional calculator using Ruby on Rails with modern UI

class CalculatorController < ApplicationController
  before_action :set_memory
  
  def index
    @display = "0"
    @previous = nil
    @operation = nil
    @history = CalculationHistory.order(created_at: :desc).limit(10)
  end
  
  def calculate
    operation = params[:operation]
    first_number = params[:first_number].to_f
    second_number = params[:second_number].to_f
    
    result = case operation
             when 'add'
               first_number + second_number
             when 'subtract'
               first_number - second_number
             when 'multiply'
               first_number * second_number
             when 'divide'
               second_number != 0 ? first_number / second_number : "Error"
             when 'power'
               first_number ** second_number
             when 'sqrt'
               Math.sqrt(first_number.abs)
             when 'sin'
               Math.sin(first_number * Math::PI / 180)
             when 'cos'
               Math.cos(first_number * Math::PI / 180)
             when 'tan'
               Math.tan(first_number * Math::PI / 180)
             when 'log'
               Math.log10(first_number)
             when 'ln'
               Math.log(first_number)
             when 'factorial'
               factorial(first_number.to_i)
             else
               "Invalid operation"
             end
    
    # Save to history
    CalculationHistory.create!(
      expression: "#{first_number} #{get_operation_symbol(operation)} #{second_number if operation != 'sqrt' && operation != 'sin' && operation != 'cos' && operation != 'tan' && operation != 'log' && operation != 'ln' && operation != 'factorial'}",
      result: result,
      operation: operation
    )
    
    respond_to do |format|
      format.html { redirect_to root_path, notice: "Calculation complete!" }
      format.json { render json: { result: result } }
    end
  end
  
  def memory
    action = params[:memory_action]
    value = params[:value].to_f
    
    case action
    when 'clear'
      session[:memory] = 0
    when 'add'
      session[:memory] = (session[:memory] || 0) + value
    when 'subtract'
      session[:memory] = (session[:memory] || 0) - value
    when 'recall'
      @memory_value = session[:memory] || 0
    end
    
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { render json: { memory: session[:memory] || 0 } }
    end
  end
  
  def history
    @history = CalculationHistory.order(created_at: :desc).limit(50)
    
    respond_to do |format|
      format.html
      format.json { render json: @history }
    end
  end
  
  private
  
  def set_memory
    @memory = session[:memory] || 0
  end
  
  def get_operation_symbol(operation)
    case operation
    when 'add' then '+'
    when 'subtract' then '-'
    when 'multiply' then '×'
    when 'divide' then '÷'
    when 'power' then '^'
    when 'sqrt' then '√'
    when 'sin' then 'sin'
    when 'cos' then 'cos'
    when 'tan' then 'tan'
    when 'log' then 'log'
    when 'ln' then 'ln'
    when 'factorial' then '!'
    else operation
    end
  end
  
  def factorial(n)
    return 1 if n <= 1
    (1..n).reduce(:*)
  end
end
124 lines•3.6 KB
ruby

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer