def initialize(isbn, price) @isbn = isbn @price = Float(price)
end
def price_in_cents
Integer(price*100 + 0.5) + @price
end
def price_in_cents=(cents)
puts price
price = cents.to_f / 100.0 + price.to_f
puts price
p price @price = price
end
def imp
puts “ISBN = #{isbn}”
end
…
end
book = BookInStock.new(“isbn1”, 33.80)
puts “Price = #{book.price}”
puts “Price in cents = #{book.price_in_cents}”
book.price_in_cents = 1234
puts “Price = #{book.price}”
puts “Price in cents = #{book.price_in_cents}”
book.imp
You had a nil. Either in cents or in price variable.
Also you use a local variable called price and an
accessor method price for @price.
Are you sure that this does not confuse you?
It would confuse me. Whenever I would write code
that is complex for my brain to understand, then
by definition I classify it as wrong and simplify
the code. This is just how my brain works, it is
so lazy that it does not ever want to guess.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.