# Asigning values to atributies when source is nil

**URL:** <https://www.ruby-forum.com/t/asigning-values-to-atributies-when-source-is-nil/99555>\
**Category:** Rails\
**Created:** [January 31, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/asigning-values-to-atributies-when-source-is-nil/99555 "2006-01-31T21:19:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_BSByrne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@James\_BSByrne](https://www.ruby-forum.com/u/James_BSByrne)\
**Post date:** [January 31, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/asigning-values-to-atributies-when-source-is-nil/99555/1 "2006-01-31T21:19:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_BSByrne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@James\_BSByrne](https://www.ruby-forum.com/u/James_BSByrne)\
**Post date:** [February 9, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/asigning-values-to-atributies-when-source-is-nil/99555/2 "2006-02-09T17:20:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_BSByrne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@James\_BSByrne](https://www.ruby-forum.com/u/James_BSByrne)\
**Post date:** [January 31, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/asigning-values-to-atributies-when-source-is-nil/99555/3 "2006-01-31T23:45:26Z")

</div>

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](http://media.pragprog.com/titles/rails/code/depot_final/app/models/cart.rb)

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