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
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
ruby-calculator
/
app
/
helpers
RSK World
ruby-calculator
Ruby Calculator Pro - Interactive Calculator with 8 Types + Mathematical Functions + Rails MVC + Modern Web Interface + API Integration + Educational Design
helpers
  • calculator_helper.rb4.8 KB
calculator_helper.rb
app/helpers/calculator_helper.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

module CalculatorHelper
  def get_operation_color(operation)
    colors = {
      'addition' => 'success',
      'subtraction' => 'danger',
      'multiplication' => 'warning',
      'division' => 'info',
      'scientific' => 'primary',
      'currency' => 'success',
      'unit' => 'secondary',
      'memory' => 'dark',
      'graphing' => 'info',
      'equation' => 'primary',
      'programming' => 'warning',
      'financial' => 'success',
      'health' => 'danger',
      'statistics' => 'info',
      'matrix' => 'secondary'
    }
    colors[operation] || 'primary'
  end
  
  def format_number(number)
    if number.is_a?(Float)
      if number.finite?
        if number == number.to_i
          number.to_i.to_s
        else
          sprintf('%.4f', number).gsub(/\.?0+$/, '')
        end
      else
        number.to_s
      end
    else
      number.to_s
    end
  end
  
  def time_ago_in_words_short(time)
    diff = Time.current - time
    
    if diff < 60
      "#{diff.to_i}s ago"
    elsif diff < 3600
      "#{(diff / 60).to_i}m ago"
    elsif diff < 86400
      "#{(diff / 3600).to_i}h ago"
    else
      "#{(diff / 86400).to_i}d ago"
    end
  end
  
  def calculation_icon(operation)
    icons = {
      'addition' => 'fa-plus',
      'subtraction' => 'fa-minus',
      'multiplication' => 'fa-times',
      'division' => 'fa-divide',
      'scientific' => 'fa-square-root-alt',
      'currency' => 'fa-dollar-sign',
      'unit' => 'fa-exchange-alt',
      'memory' => 'fa-memory',
      'graphing' => 'fa-chart-line',
      'equation' => 'fa-equals',
      'programming' => 'fa-code',
      'financial' => 'fa-calculator',
      'health' => 'fa-heartbeat',
      'statistics' => 'fa-chart-bar',
      'matrix' => 'fa-table'
    }
    icons[operation] || 'fa-calculator'
  end
  
  def theme_color_class(theme_name)
    theme_classes = {
      'default' => 'bg-gradient-purple',
      'dark' => 'bg-gradient-dark',
      'ocean' => 'bg-gradient-ocean',
      'sunset' => 'bg-gradient-sunset',
      'forest' => 'bg-gradient-forest',
      'candy' => 'bg-gradient-candy'
    }
    theme_classes[theme_name] || 'bg-gradient-purple'
  end
  
  def favorite_category_icon(category)
    icons = {
      'mathematics' => 'fa-calculator',
      'physics' => 'fa-atom',
      'finance' => 'fa-dollar-sign',
      'engineering' => 'fa-cogs',
      'statistics' => 'fa-chart-bar',
      'conversion' => 'fa-exchange-alt',
      'health' => 'fa-heartbeat',
      'other' => 'fa-star'
    }
    icons[category] || 'fa-star'
  end
  
  def safe_eval(expression)
    # Safe mathematical expression evaluation
    # Only allow mathematical functions and operators
    allowed_chars = /^[0-9+\-*/().^sinco tan log sqrtabs pi e]+$/
    
    if expression.match?(allowed_chars)
      begin
        # Replace mathematical functions with Ruby equivalents
        processed = expression.downcase
          .gsub(/sin/, 'Math.sin')
          .gsub(/cos/, 'Math.cos')
          .gsub(/tan/, 'Math.tan')
          .gsub(/log/, 'Math.log10')
          .gsub(/sqrt/, 'Math.sqrt')
          .gsub(/abs/, 'Math.abs')
          .gsub(/pi/, 'Math::PI')
          .gsub(/e/, 'Math::E')
          .gsub(/\^/, '**')
        
        eval(processed)
      rescue => e
        "Error: #{e.message}"
      end
    else
      "Error: Invalid characters in expression"
    end
  end
  
  def calculation_statistics(history)
    return {} if history.empty?
    
    {
      total: history.count,
      by_operation: history.group(:operation).count,
      by_day: history.group_by_day(:created_at).count,
      average_result: history.average(:result)&.round(4),
      most_used_operation: history.group(:operation).count.max_by(&:last)&.first,
      recent_count: history.where('created_at > ?', 1.day.ago).count
    }
  end
  
  def export_calculations_csv(calculations)
    CSV.generate do |csv|
      csv << ['Expression', 'Result', 'Operation', 'Created At']
      calculations.each do |calc|
        csv << [calc.expression, calc.result, calc.operation, calc.created_at.strftime('%Y-%m-%d %H:%M:%S')]
      end
    end
  end
  
  def export_calculations_json(calculations)
    calculations.map do |calc|
      {
        expression: calc.expression,
        result: calc.result,
        operation: calc.operation,
        created_at: calc.created_at.iso8601
      }
    end.to_json
  end
end
168 lines•4.8 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