Asigning values to atributies when source is nil

I want to use something similar to the following:

@total_price += { product.price <> nil ? product.price : 0.0 }

This fails in rails with a “nil can’t be coerced into Float” error.
What is the (most) correct way to do this?

On a related issue, what is the significance of the value 40
following “cart.rb” in the trace snippet below? The entire class
file is much less than 40 lines long so is this a byte offset? If
so, how does one use it to locate the error in the source?

#{RAILS_ROOT}/app/models/cart.rb:40:in `add_product’

Regards,
Jim


*** e-mail is not a secure channel ***
mailto:byrnejb.@harte-lyne.ca
James B. Byrne Harte & Lyne Limited
vox: +1 905 561 1241 9 Brockley Drive
fax: +1 905 561 0757 Hamilton, Ontario
= hal Canada L8E 3C3

Mark Reginald J. wrote:

What is the (most) correct way to do this?

@total_price += product.price.to_f

Thank you very much. I missed your original reply in the mailing list
digest which accounts for my delay in responding.

Regards,
Jim

James B. Byrne wrote:

I want to use something similar to the following:

@total_price += { product.price <> nil ? product.price : 0.0 }

This fails in rails with a “nil can’t be coerced into Float” error.
What is the (most) correct way to do this?

@total_price += product.price.to_f

On a related issue, what is the significance of the value 40
following “cart.rb” in the trace snippet below? The entire class
file is much less than 40 lines long so is this a byte offset? If
so, how does one use it to locate the error in the source?

#{RAILS_ROOT}/app/models/cart.rb:40:in `add_product’

@total_price += product.price” is line 40 of
http://media.pragprog.com/titles/rails/code/depot_final/app/models/cart.rb


We develop, watch us RoR, in numbers too big to ignore.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs