undefined local variable or method

0
class ProdutItem
  attr_reader :item, :price_unit, :qtde

  def initialize(item, price_unit, qtde)
    @item = item
    @price_unit = price_unit
    @qtde = qtde
  end

  def calc_qtde
     (price_unit * qtde)
  end

end

 prod = ProdutItem.new("A", 0.50, 3)
 puts prod.calc_qtde

class CalcDiscount
  attr_reader :discount

  def initialize(discount)
    @discount = discount
  end

  def calc_desc
    (price_unit * qtde) - discount
  end
end

desc = CalcDiscount.new(0.20)
puts desc.calc_desc

When I execute this my code above returns the following error msg:

  

teste_04.rb: 29: in calc_desc': undefined local variable or method price_unit 'for #   (NameError)               from teste_04.rb: 34: in ''

Eu nao consigo identificar o erro. Alguem poderia me ajudar?
    
asked by anonymous 10.08.2017 / 21:56

1 answer

0

I did it that way and it worked:

class ProdutItem
  attr_reader :item, :price_unit, :qtde

  def initialize(item, price_unit, qtde)
    @item = item
    @price_unit = price_unit
    @qtde = qtde
  end

  def calc_qtde
    (price_unit * qtde)
  end

end

class CalcDiscount
  attr_reader :discount

  def initialize(discount)
    @discount = discount
  end

  def calc_desc(product)
    (product.price_unit * product.qtde) - discount
  end
end


prod = ProdutItem.new("A", 0.50, 3)
puts prod.calc_qtde

desc = CalcDiscount.new(0.20)
puts desc.calc_desc(prod)

To calculate the discount, the method needs the unit price and the quantity of the product. Because of this, I added the product parameter in the method that calculates the discount so we can access the information we want.

    
19.08.2017 / 17:31