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
......

44 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.

poll 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.

poll 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.

poll 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.

poll 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...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店

禮服酒店

酒店兼差

酒店上班

酒店PT

酒店經紀

酒店喝酒

酒店消費

喝花酒

粉味

喝酒

rr8004 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.

rr8004 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 Dallas Business Directory when you got time.

rr8004 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

rr8004 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...

Congratulations, you just earned yourself an entry in my feed reader, great blog. Please come visit my site Columbus Phone Book when you got time.

rr8004 said...

Congratulations, you just earned yourself an entry in my feed reader, great blog. Please come visit my site Columbus Business Directory Forum Blog Classifieds 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...

I really liked your post on home financing! if you have more information elsewhere let me know. Please come visit my site Arlington Business Directory Forum Blog Classifieds 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.

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 City Guide Memphis when you got time.

rr8004 said...

She’s really interested in this subject. Thanks again… Please come visit my site Garland Business Services And Classifieds when you got time.

rr8004 said...

She’s really interested in this subject. Thanks again… Please come visit my site Texas TX Phone Directory when you got time.

良哥 said...

Teenagers in a track adidas shoes can go through shoes rate adidas outlet teenage runner well knows that adidas sneakers to wear the right shoes air jordan 2010 any cross country among world jordan shoes especially important to the sportsman discount jordan shoes a young person growing up Pure GHD Not only do they want to bourke dooney handbags grow out of their shoes dooney bourke bags but shape of their feet changes which means every time we dooney bourke outlet the time comes to get dooney bourke totes a new pair of shoes eveyone NIKE SHOX a good choice to get NIKE SHOX SHOES it with a high material SHOX SHOES that breathes easily on random moncler They should be extended runs moncler jackets give enough support to athlets moncler coats especially around the ankles area moncler vest to avoid injury your ankles discount moncler outlet when you make your trip discount moncler t-shirt something in store possible fit cheap ugg boots his is also the way to discount ugg boots that don't fit properly only ugg boots it focuses on your convenience classic ugg boots but they are not returnable ugg classic tall boots

Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班 酒店兼差 酒店兼職 打工兼差 打工兼職 台北酒店 酒店應徵 禮服酒店 酒店 經紀 打工 兼差

polo shirts said...

History of polo ralph lauren. Polo fashions had its humble beginnings in 1968 when tie salesman Ralph Lauren gave it a kick start. By 1969 he had a boutique polo ralph lauren factory stores within the Manhattan department store Bloomingdale's. ... Brands and luxury standard. Since Ralph Lauren's first brand, Polo Ralph Lauren, was launched, the company has expanded to include a variety of luxury brands such as Polo Golf, Polo Denim, Polo Sport. You can buy cheap Ralph Lauren Clothing at Ralph Lauren outlet.Also We provide polo shirts
Ralph Lauren polo shirt, 50% OFF! polo ralph lauren outlet online is your best choice!In 2006, polo ralph lauren outlet became the first designer in Wimbledon's 133-year history to create official uniforms for the tournament. As part of this year's event, which starts next week, polo ralph lauren sale will introduces the first ... determination to maintain and enhance the values for which our two brands are famous throughout the world. The rugby ralph lauren brand brings to Wimbledon the look of timeless elegance, drawing on our rich history and traditions

dfadf said...

Y te vi bailar bajo la lluvia
y saltar sobre un charco de estrellas
Obama Is Lying<1>
When was the last time the MSM took a Republican's side in a fight over credibility with a Democratic opponent?Microsoft OfficeOffice 2010Well, it has been a while.Microsoft Office 2010However, conservatives have little to grumble about in the recent Office 2007face-off between Barack Obama and John McCain over McCain's statement thatte vi bailar bajo la lluvia
esperando la luna llena
Volverás a reírte de veras
Microsoft Office 2007troops might have to remain for "100 years" in Iraq "as long as Americans are not being injured or harmed or wounded or killed" afterOffice 2010 keyfighting had concluded, Office 2010 downloadMost recently, ABC's Jake Tapper noted that at least on three occasions Obama had personally said that McCain favored cuando creas que estaba perdidovolverás a reírte de veras
continued fighting in Office 2010 ProfessionalIraq for 100 years. Tapper concluded that "Obama has in the past distorted McCain's comments" and "that he is violating his own stated si te quedas conmigoTe vi bailar bajo la lluviaesperando la luna llente vi llorar bajo la lunMicrosoft outlookaspirations...[b]ecause not only has he distorted what McCain said, he is not being honest about having made those distortions."Outlook 2010Tapper is not the only MSM reporter to point this out, Windows 7of course. It is not every day that the RNC sends around e-mail blasts quoting Frank Microsoft outlook 2010Rich ("Barack Obama and Hillary Clinton should be ashamed of themselves for libeling John McCain") and reports from the Chicago Tribune,

ektas said...

Nice, I think it could be interesting to add some more entries following this one. thanks for all that..
ses kayıt
ses kayıt
ses kayıt sistemi

lanlan 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

Frank 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

clarebear said...

We are China online Wholesale Beads store, China Wholesale Beads is your best choice. China jewelry findings and jewelry making supplies for wholesale.We offer you Jewelry Supplies, Jewelry Findings, Jewelry Beads at the cheapest price. We are specialized in Wholesale Jewelry Supplies, Wholesale Jewelry Findings, Tibetan Style Beads, Tibetan Silver Beads, Tibetan Silver Charms, Wholesale Acrylic Beads, Wholesale Crystal Beads, Wholesale Jewelry Charms, Pandora Style Beads, Lampwork Beads, Metal Beads, Gemstone Beads, Wood & Nut Beads. Buy more get more discount.

Anonymous said...

Counter Depth French Door Refrigerator
How to Get Into Acting
Breville Smart Oven
How to Impress Woman
Picking Lottery Numbers
Canon Powershot A800
Adidas Barricade
Adidas Barricade 6.0
Cuisinart TOB-195
Acne Conglobata
Acer 11.6 Netbook
500 payday loan
acne inversa
VIZIO M261VP
checkmate payday loans
Pengurusan Masa
Counter Depth French Door Refrigerator
How to Get Into Acting
Breville Smart Oven
How to Impress Woman
Picking Lottery Numbers
Canon Powershot A800
Adidas Barricade 6.0
Cuisinart TOB-195
Acne Conglobata
Acer 11.6 Netbook
500 payday loan
acne inversa
VIZIO M261VP
checkmate payday loans
Pengurusan Masa

elizabeth bernot said...

Costa Rica Fishing

http://www.fishingcostaricaexperts.com/

Fishing Costa Rica

Anonymous said...

Counter Depth French Door Refrigerator
How to Get Into Acting
Breville Smart Oven
How to Impress Woman
Picking Lottery Numbers
Canon Powershot A800
Adidas Barricade
Cuisinart TOB-195
Acne Conglobata
Acer 11.6 Netbook
500 payday loan
acne inversa
VIZIO M261VP
checkmate payday loans
Pengurusan Masa
How To Stop Panic Attacks
Didik anak
Langsing
Adidas Barricade 6.0
Adidas Barricade V

wow gold said...

RS Gold
Tera Power Leveling
Buy RS Gold

Anonymous said...

Cheap Gold for WOW |subing

Cheapwowgold 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.

FZF said...

Thanks for finding the time to discuss this, I am strongly about it and love learning more about this topic. If possible, when you gain expertise, does one mind updating your blog site to comprehend information? It is very of great help for me.
http://gameplayingspots.blog.com/2011/12/21/the-transition-will-begin-in-january-and-be-completed-by-march/
http://www.vibesconnect.com/gamespots/blog/NASA-scientists-today-announce,58164
http://gamespots.centerblog.net/15-ncaa-imposes-penalties-beyond-self-imposed-by-the-university
http://gamespots.beeplog.com/blog.pl?blogid=225704&o=entry&a=add
http://gamespots.blogg.se/?tmp=21035040
http://gamespots.eklablog.com/amish-men-s-beards-and-amish-women-s-hair-is-a-symbol-of-their-faith-a27555100

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

love game 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.

Ding Michelle 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

Winnie Qin said...

Thank you for sharing this.To Your Success!






-----------------------------------
Discount Abercrombie Clothing;Juicy Couture Womens suit

Jerry Gene said...

Nice post! Can’t wait for the next one. Keep stuff like this coming. Nice Post keep it up.

Asus - N55SL-DS71 15.6" LED Notebook - Intel Core i7 i7-2670QM 2.20 GHz - Black

Asus - 11.6"ZENBOOK Ultrabook 4 GB Memory and 128 GB Solid State Drive - Aluminum Silver

Asus - 13.3" Laptop - 4GB Memory - 128GB Solid State Drive - Hot Pink

Asus - 11.6" Laptop - 4GB Memory - 128GB Solid State Drive - Radiant Silver

Asus - 15.6" Notebook 8 GB Memory - 500 GB Hard Drive - Black