# Difference between nil? vs. (variable != nil)

**URL:** <https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532>\
**Category:** Ruby\
**Created:** [December 6, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532 "2006-12-06T18:21:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/1 "2006-12-06T18:21:15Z")

</div>

Source A :

if (@product\_coverages != nil && @product\_coverages.length \> 0)  
@product\_coverages = @product\_coverages.each do  
|product\_coverage|  
product\_coverage.external\_description =  
Coverage.find(product\_coverage.coverage\_id).external\_description # test  
if (product\_coverage.external\_description != nil)  
product\_coverage.external\_description.capitalize!  
end  
end  
end

Source B :

if (@product\_coverages != nil && @product\_coverages.length \> 0)  
@product\_coverages = @product\_coverages.each do  
|product\_coverage|  
product\_coverage.external\_description =  
Coverage.find(product\_coverage.coverage\_id).external\_description # test  
return “” if product\_coverage.external\_description.nil?  
product\_coverage.external\_description.capitalize!  
end  
end

I wanted to use Source B, but it return “” everytime. But Source A  
works. I expected both of them to work.

Jason

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/2 "2006-12-06T18:37:20Z")

</div>

On 12/6/06, Jason Vogel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> product_coverage.external_description.capitalize!
> end
> end
> 
> ```
> 
> I wanted to use Source B, but it return “” everytime. But Source A  
> works. I expected both of them to work.

Put in a test print, to see what nil? is returning. It is possible for  
a class to override nil?, though it’s hard to think of why one would.

martin

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/3 "2006-12-06T20:55:58Z")

</div>

If you put those source in a loop, source B always return “” if  
product\_coverage.external\_description == nil while source A does not.  
Basically those sources do different job .

On 12/6/06, Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > product_coverage.external_description.capitalize!
> > 
> > ```
> > 
> > Coverage.find(product\_coverage.coverage\_id).external\_description # test
> 
> martin

–  
Chá»‰ cÃ³ lÃ½ trÃ­ cá»§a báº£n thÃ¢n má»›i soi sÃ¡ng Ä‘Æ°á»£c sá»± tháº­t.“CÃ¡i Ä‘áº¹p Ä‘áº½ nháº¥t mÃ  
chÃºng ta cÃ³ thá»ƒ tráº£i nghiá»‡m Ä‘Æ°á»£c lÃ cÃ¡i bÃ­ áº©n. ÄÃ³ lÃ cáº£m thá»©c ná»n táº£ng  
trong  
cÃ¡i nÃ´i cá»§a nghá»‡ thuáº­t vÃ khoa há»c chÃ¢n chÃ­nh. Káº» nÃ o khÃ´ng biáº¿t Ä‘áº¿n nÃ³,  
khÃ´ng cÃ²n kháº£ nÄƒng ngáº¡c nhiÃªn hay kinh ngáº¡c, káº» Ä‘Ã³ coi nhÆ° Ä‘Ã£ cháº¿t, Ä‘Ã£  
táº¯t  
rá»¥i lá»­a sá»‘ng trong máº¯t mÃ¬nh”“Das SchÃ¶nste, was wir erleben kÃ¶nnen, ist  
das  
Geheimnisvolle. Es ist das GrundgefÃ¼hl, das an der Wiege von wahrer  
Kunst  
und Wissenschaft steht. Wer es nicht kennt und sich nicht mehr wundern,  
nicht mehr staunen kann, der ist sozusagen tot und sein Auge  
erloschen.”“The  
fairest thing we can experience is the mysterious. It is the fundamental  
emotion which stands at the cradle of true art and true science. He who  
knows it not and can no longer wonder, no longer feel amazement, is as  
good  
as dead, his eyelight has been extinct.”

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 7, 2006, 6:45am UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/4 "2006-12-07T06:45:33Z")

</div>

I found ‘return “” if product\_coverage.external\_description.nil?’ as a  
source sample somewhere, and I liked the readability factor. It seemed  
very “Ruby-ish” to a PowerBuilder/Java/C# developer :-).

In the end, I’m trying to figure out my “best practice” for iterating  
over an array of objects. I’ll happily take any suggestions.

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 7, 2006, 7:57am UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/5 "2006-12-07T07:57:40Z")

</div>

James M. wrote:

> Is there a particular reason you’re comparing to nil? Normally I’d just  
> write something like
> 
> (@product\_coverages && @product\_coverages.length \> 0)

This is Perlish, and there’s people (myself included) that don’t like  
the style - it hides the nil-check, which is useful to be more visible  
if you’re doing it as a special case filter, like snippet B in the  
original post does. I usually prefer getting rid of special cases like  
missing arguments etc. with a series of checks and returns / exceptions  
in the worse cases at a the beginning of a method to keep precondition  
verification in one place instead of scattered through the code with  
failure consequences interspersed with regular processing.

David V.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 10, 2006, 7:36am UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/6 "2006-12-10T07:36:47Z")

</div>

I appreciate the advice. I’ve just being trying to be careful to not  
be too C#/Java-ish. I’ve never done any Perl.

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 10, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/7 "2006-12-10T16:51:23Z")

</div>

Jason V. wrote:

> I appreciate the advice. I’ve just being trying to be careful to not  
> be too C#/Java-ish. I’ve never done any Perl.

Huh? Why not? You should write that a) you can read easily, b) other  
people working on the codebase can read easily.

The “Approved By The Grand Commitee Of Java Bashers ®” stamp comes, in  
my opinion, very, very far behind. Consider the practical criteria  
first.

David V.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 10, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/8 "2006-12-10T18:45:34Z")

</div>

Jason Vogel wrote:

> ```
> end
> product_coverage.external_description.capitalize!
> end
> end
> 
> ```
> 
> I wanted to use Source B, but it return “” everytime. But Source A  
> works. I expected both of them to work.
> 
> Jason

if @product\_coverages != nil  
@product\_coverages.each { |p\_c|  
p\_c.external\_description =  
Coverage.find(p\_c.coverage\_id).external\_description and  
p\_c.external\_description.capitalize!  
}  
end

* * *

A simpler way. When a description that’s nil is encountered,  
replace it with “”.

if @product\_coverages != nil  
@product\_coverages.each { |p\_c|  
p\_c.external\_description =  
(Coverage.find(p\_c.coverage\_id).external\_description or “”).  
capitalize  
}  
end

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/difference-between-nil-vs-variable-nil/78532/9 "2006-12-06T21:32:16Z")

</div>

Is there a particular reason you’re comparing to nil? Normally I’d just  
write something like

(@product\_coverages && @product\_coverages.length \> 0)

- James M.
