Sunday, May 20, 2007

Can you spot the bug?

In a model class which has 'quantity', 'quantity_already_taken' and 'quantity_requested' properties, I add the following:

def before_save
quantity = 1 if quantity == 0
if quantity + quantity_already_taken > quantity_requested
......

Tests blow up everywhere with:

TypeError: nil can't be coerced into Fixnum
(on the line with the addition)

Why?

After a little more debugging, it is clear that 'quantity' is nil. How could that happen?

The answer lies in the fact that Ruby requires an explicit self reference when using attribute writers (aka, property setters) within the class itself. This feels clunky to me, but for your information, here's a rationalisation of the explicit self requirement.

So, in case you're wondering, what happened above is that the 'if' line created a nil local variable called 'quantity'! This local variable then had higher scope precedence than the class attribute with the same name. The addition line was then using the local 'quantity' rather than the class attribute and hence failed with the nil error.

All fixed by explicity referencing self:

def before_save
self.quantity = 1 if (quantity == 0)
if quantity + quantity_already_taken > quantity_requested
......

26 comments:

Wholesale Fashion Watches said...

I was thinking of looking up some of them newspaper websites, but am glad I came here instead. Although glad is not quite the right word… let me just say I needed this after the incessant chatter in the media, and am grateful to you for articulating something many of us are feeling - even from distant shores. Feel free to check out my site fashion accessory when you got time.

wholesale gift shop said...

You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through. I would love some feedback on my site garden supply when you got time.

Anonymous said...

Thanks for posting, definitely going to subscribe! See you on my reader. Please come visit my site Phone Directory Of Colorado Springs City Colorado CO State when you got time.

Anonymous said...

Thanks for posting, definitely going to subscribe! See you on my reader. Please come visit my site Aurora Colorado CO when you got time.

Anonymous said...

As a Newbie, I am always searching online for information that can help me. Thank you. Please come visit my site Austin Directory when you got time.

Anonymous said...

As a Newbie, I am always searching online for information that can help me. Thank you. Please come visit my site Austin Texas TX when you got time.

Anonymous said...

I am not really sure if best practices have emerged around things like that, but I am sure that your great job is clearly identifed. I was wondering if you offer any subscription to your RSS feeds as I would be very interested and can’t find any link to subscribe here. Please come visit my site Directory San Diego City when you got time.

Anonymous said...

. Since I’m new to blogging, these articles are greatly appreciated; very useful and informative blog and every body must visit this blog. Please come visit my site Corpus Christi Business Services And Classifieds when you got time

Anonymous said...

. Since I’m new to blogging, these articles are greatly appreciated; very useful and informative blog and every body must visit this blog. Please come visit my site Texas TX Phone Directory when you got time

rr8004 said...

I really liked your post on home financing! if you have more information elsewhere let me know. Please come visit my site Arlington Phone Book when you got time.

rr8004 said...

This is just another reason why I like your website. I like your style of writing you tell your stories without out sending us to 5 other sites to complete the story. Please come visit my site Memphis Business Directory Forum Blog Classifieds when you got time.

weddingdresses said...

Are you preparing Christmas gifts for your loves ,family and friends now ?
Maybe you can choose us , we always provide the excellent quality products and best after-sale service ,

recently we have a active promotions :Happy Shopping at Dec,Free shipping!10%~15% discount.
we are accept paypal ,credit card and western union for payment .
New arrival :Power Balance
winter boots
power balance bracelet
Silly Bands
Reebok Easytone
reebok zigtech
efx bracelet
Moncler Jackets
Log in website :www.winterboots2sale.com
www.brandshoes2trade.com
www.power2balance.com

Unknown said...

All those sequels will be fed to Starz as part of the new distribution deal. Other Weinstein Co. films coming to Starz include critically acclaimed "The King's Speech," and "Blue Valentine." nice and cheap ugg boots is on display right now at Harrods ugg clearance in London, but once released, will there be a market for it? The new green luxury car gives the environmentally conscious executive cheap uggs clearance the option of powering the CT 200H by either electricity orjordan basketball shoes by regular petrol. Lexusugg women is banking on the belief that there is a growing demand for a greenJordan Fusions. vehicle in the luxury car market.

Dylan Hall said...

Your summaries are always top-notch. Thanks for keeping us apprised. I’m reading every word here.

dodge parts

wow gold said...

RS Gold
Tera Power Leveling
Buy RS Gold

Anonymous said...

Cheap Gold for WOW |subing

Anonymous said...

For this reason players have a tendency to be tempted to get Gold WOW through safe web internet sites. Unless you will be an achieved player, accumulating the Gold for WOW you will need could possibly be a frustrating procedure.

Anonymous said...

I was thinking of looking up some of them newspaper websites, but am glad I came here cambridge batchel instead. Although glad is not quite the right word… let me just say I needed this after the incessant chatter in the media, and am grateful to you for articulating something many of us are feeling pink cambridge satchel- even from distant shores.

glyco flex said...

Damn I wasn't able to spot it without your help.

tissot men said...

Really trustworthy blog. I love it every much, and i have same style blog you can visit and give some advices, than you!tissot
tissot watch
sport tissot
tissot ladies watch
tissot women watches
tissot for women

Anonymous said...

So, in situation you are wondering,Cheap RS Gold what took place previously is the fact that 'if' level developed a nil neighborhood varying called 'quantity'! This neighborhood varying then experienced higher scope precedence compared to course attribute using the specific same name. The inclusion level was then employing the neighborhood 'quantity' as opposed to the course attribute and therefore failed Sell Runescape Goldusing the nil error.

wedding dress said...

Additional tones are utilized simply because these types of colours tend to be ideal for official events for example graduating, as well as these types of can function like a celebration colour as well. The actual designs for example teas duration drink gowns along with stunning necklines as well as beautiful drops could be a couple of options for your own graduating gowns Alfred Angelo. Because these types of possess official as well as informal designs come up with, they may be an ideal options in order to move on within! To place more styles before you decide to, we now have put together a few info comparable within the sentences beneath. Have a look as well as help to make your decision.

Unknown said...

You'll be able for the handles to be on major or on the facets. One aspect bracket can come within bathroom sink faucets deigns. Are probably the largest article will surely have 2 corner grips whilst the centerset comes with a lever tackle that's connected to the sink, and also the link touch can led faucet be a one piece light fixture that really needs a couple of slots.

Anonymous said...

I have a question about http://jamescrisp.blogspot.co.uk/2007/05/can-you-spot-bug.html Please contact me so I can discuss with you. - amanda@vetdepot.com

Tejuteju said...

Nice post! Thanks for sharing valuable information with us. Keep sharing...Ruby on Rails Online Course India

5689 said...

zzzzz2018.9.6
giuseppe zanotti
michael kors outlet
golden goose
moncler outlet
ralph lauren
pandora
adidas superstar
kate spade handbags
pandora jewelry
jordan uk