Ruby 代码格式化工具

专业级的Ruby代码格式化与美化

class Calculator
  def initialize
    @result = 0
  end

  def add(num)
    @result += num
  end

  def subtract(num)
    @result -= num
  end

  def calculate(items)
    items.each do |item|
      if item[:price] > 100
        @result += item[:price] * 0.9  # 10% discount
      else
        @result += item[:price]
      end
    end
    @result
  end
end

cart_items = [
  { name: 'Laptop', price: 1200 },
  { name: 'Mouse', price: 25 },
  { name: 'Keyboard', price: 60 }
]

calc = Calculator.new
puts "Total: #{calc.calculate(cart_items)}"