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
/
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
LICENSEcontent_security_policy.rbindex.html.erbcalculator_helper.rb
node_modules/is-extglob/LICENSE
Raw Download
Find: Go to:
The MIT License (MIT)

Copyright (c) 2014-2016, Jon Schlinkert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
22 lines•1.1 KB
text
config/initializers/content_security_policy.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

# Be sure to restart your server when you modify this file.

# Define an application-wide content security policy.
# See the Securing Rails Applications Guide for more information:
# https://guides.rubyonrails.org/security.html#content-security-policy-header

# Rails.application.configure do
#   config.content_security_policy do |policy|
#     policy.default_src :self, :https
#     policy.font_src    :self, :https, :data
#     policy.img_src     :self, :https, :data
#     policy.object_src  :none
#     policy.script_src  :self, :https
#     policy.style_src   :self, :https
#   end
#
#   # Generate session nonces for permitted importmap and inline scripts
#   config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
#   config.content_security_policy_nonce_directives = %w(script-src)
#
#   # Report CSP violations to a specified URI
#   # config.content_security_policy_report_only = true
#   # config.content_security_policy_report_uri = "/csp-violation-report-endpoint"
# end
32 lines•1.3 KB
ruby
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