# Var = nil if false; p var.nil? #=\> true | bug of the day?

**URL:** <https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474>\
**Category:** Ruby\
**Created:** [April 11, 2008, 7:15am UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474 "2008-04-11T07:15:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arie\_Kusuma\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d26b3c/32.png) [@Arie\_Kusuma\_A](https://www.ruby-forum.com/u/Arie_Kusuma_A)\
**Post date:** [April 11, 2008, 7:15am UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474/1 "2008-04-11T07:15:16Z")

</div>

I tested it using Ruby 1.8.6 patch 111 and also Ruby 1.9.0 (revision  
15968).

var = nil if false  
var.nil? #=\> true

I actually saw it from tobi’s blog, then I discussed with a perler who  
loves ruby as well, here’s a copy paste. (Bottomline: is it a feature  
or a bug???)

12:52:37 PM edpratomo: kok bisa gitu? (yg irb itu)  
12:52:49 PM Rie!: bug of the day -\_-  
12:52:56 PM edpratomo: ruby 1.9?  
12:53:28 PM Rie!: 1.8.6 as well  
12:55:09 PM edpratomo: oh i understand  
12:55:31 PM edpratomo: var = nil if false creates var  
12:56:01 PM Rie!: it’s just like the stupid param variable is still  
available outside of the block/yield  
12:56:07 PM edpratomo: irb(main):005:0\> var = “blah” if false  
=\> nil  
irb(main):006:0\> var  
=\> nil

12:56:32 PM edpratomo: so it’s not a bug  
12:56:58 PM edpratomo: ever got bitten by the leaky block scope?  
12:57:18 PM Rie!: how come it’s not a bug :-B var should be undefined  
local var  
12:57:20 PM Rie!: not nil  
12:57:51 PM edpratomo: try to post that to ruby-talk 🙂 i believe you  
will get “it’s not a bug” replies  
12:58:11 PM Rie!: that’s from tobias luetjke blog  
12:58:20 PM Rie!: so i believe many rubyists had noticed that  
12:58:46 PM edpratomo: has been discussed in ruby-talk/core?  
12:58:58 PM Rie!: haven’t checked yet  
1:00:02 PM edpratomo: imho it’s not a bug. that foo = nil if false  
creates unitialized foo.  
1:00:31 PM edpratomo: in perl iirc such expression will be caught  
1:00:39 PM Rie!: well it shouldn’t execute the command inside that if  
false  
1:01:04 PM edpratomo: if the if clause is evaluated first.  
1:03:56 PM edpratomo: perl -wle’my $foo = “ok” if 0; print $foo’  
Use of uninitialized value in print at -e line 1.

1:04:45 PM edpratomo: perl -Mstrict -wle’my $foo = “ok” if 0; print  
$foo’  
Use of uninitialized value in print at -e line 1  
1:05:03 PM edpratomo: it’s a warning. not error.  
1:05:22 PM edpratomo: so $foo is created, but unitialized.  
1:05:38 PM edpratomo: in perl it will issue warning

[in bahasa indonesia]

Saya sebetulnya melihat ini dari blog Tobi kemudian saya berdiskusi  
bersama seorang pengguna perl yang juga mencintai ruby, berikut tadi  
copy paste nya. (Inti dari pertanyaan saya adalah apakah ini fitur  
ataukah bug di ruby???)

–  
blog: [http://tinyurl.com/2bjgvn](http://tinyurl.com/2bjgvn),  
ruby: [Tentang Ruby](http://www.ruby-lang.org/id/about)

“Developer Time is expensive…  
Servers are cheap…”, Ezra Z. - Merb

---

<div class="post-metadata">

**Author:** ![Arie\_Kusuma\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d26b3c/32.png) [@Arie\_Kusuma\_A](https://www.ruby-forum.com/u/Arie_Kusuma_A)\
**Post date:** [April 11, 2008, 7:25am UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474/2 "2008-04-11T07:25:14Z")

</div>

On Apr 11, 2008, at 12:14 AM, Arie Kusuma A. wrote:

> I tested it using Ruby 1.8.6 patch 111 and also Ruby 1.9.0 (revision  
> 15968).
> 
> var = nil if false  
> var.nil? #=\> true

Ruby sees you have a local variable called ‘var’. It hasn’t been given  
a value, and so is nil.

Dave

---

<div class="post-metadata">

**Author:** ![Arie\_Kusuma\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d26b3c/32.png) [@Arie\_Kusuma\_A](https://www.ruby-forum.com/u/Arie_Kusuma_A)\
**Post date:** [April 11, 2008, 7:37am UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474/3 "2008-04-11T07:37:31Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Arie Kusuma A. wrote:  
| I tested it using Ruby 1.8.6 patch 111 and also Ruby 1.9.0 (revision  
15968).  
|  
| var = nil if false  
| var.nil? #=\> true  
|  
| I actually saw it from tobi’s blog, then I discussed with a perler who  
| loves ruby as well, here’s a copy paste. (Bottomline: is it a feature  
| or a bug???)

irb(main):001:0\> something = “something” if false  
=\> nil  
irb(main):002:0\> something\_else = nil  
=\> nil  
irb(main):003:0\> something\_else.class  
=\> NilClass  
irb(main):004:0\> exit

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)

~ My life needs a rewind/erase button.  
– Calvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkf++QEACgkQbtAgaoJTgL/0pgCgpnN3q2Ka1pPXwa7M4HmDZEvW  
1t8AoJtxM8KASS4LHSmXe57vrzYC1Ney  
=vxup  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Arie\_Kusuma\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d26b3c/32.png) [@Arie\_Kusuma\_A](https://www.ruby-forum.com/u/Arie_Kusuma_A)\
**Post date:** [April 11, 2008, 9:18am UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474/4 "2008-04-11T09:18:44Z")

</div>

Arie Kusuma A. wrote:

> I tested it using Ruby 1.8.6 patch 111 and also Ruby 1.9.0 (revision  
> 15968).
> 
> var = nil if false  
> var.nil? #=\> true

irb(main):001:0\> a = 1  
=\> 1  
irb(main):002:0\> a = nil if false  
=\> nil  
irb(main):003:0\> a  
=\> 1  
irb(main):004:0\> b  
NameError: undefined local variable or method `b’ for main:Object  
from (irb):4  
irb(main):005:0\> b = nil if false  
=\> nil  
irb(main):006:0\> b  
=\> nil

There is some salt in the problem althow I don’t think that it makes any  
troubles in real life.

by  
TheR

---

<div class="post-metadata">

**Author:** ![Arie\_Kusuma\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d26b3c/32.png) [@Arie\_Kusuma\_A](https://www.ruby-forum.com/u/Arie_Kusuma_A)\
**Post date:** [April 11, 2008, 11:58am UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474/5 "2008-04-11T11:58:39Z")

</div>

2008/4/11, Phillip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> | I actually saw it from tobi’s blog, then I discussed with a perler who  
> | loves ruby as well, here’s a copy paste. (Bottomline: is it a feature  
> | or a bug???)
> 
> irb(main):001:0\> something = “something” if false  
> =\> nil  
> irb(main):002:0\> something\_else = nil  
> =\> nil  
> irb(main):003:0\> something\_else.class  
> =\> NilClass  
> irb(main):004:0\> exit

Be careful when testing issues that involve local variables in IRB.  
Top level local variables such as these do behave differently in IRB.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Arie\_Kusuma\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d26b3c/32.png) [@Arie\_Kusuma\_A](https://www.ruby-forum.com/u/Arie_Kusuma_A)\
**Post date:** [April 11, 2008, 9:32am UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474/6 "2008-04-11T09:32:02Z")

</div>

On 11/04/2008, Dave T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> value, and so is nil.  
> Ok, I got it, thanks Dave.

m:x arie$ irb19  
irb(main):001:0\> a = 1  
=\> 1  
irb(main):002:0\> a  
=\> 1  
irb(main):003:0\> a = nil if false  
=\> nil  
irb(main):004:0\> exit  
m:x arie$ irb19  
irb(main):001:0\> a = nil if false  
=\> nil  
irb(main):002:0\> a  
=\> nil  
irb(main):003:0\> exit  
m:x arie$ irb19  
irb(main):001:0\> a  
NameError: undefined local variable or method `a' for main:Object from (irb):1 from /opt/local/ruby19/bin/irb19:12:in `’  
irb(main):002:0\> a = ‘nothing’ if false  
=\> nil  
irb(main):003:0\> a  
=\> nil  
irb(main):004:0\> exit

> Dave

–  
blog: [http://tinyurl.com/2bjgvn](http://tinyurl.com/2bjgvn),  
ruby: [Tentang Ruby](http://www.ruby-lang.org/id/about)

“Developer Time is expensive…  
Servers are cheap…”, Ezra Z. - Merb

---

<div class="post-metadata">

**Author:** ![Arie\_Kusuma\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d26b3c/32.png) [@Arie\_Kusuma\_A](https://www.ruby-forum.com/u/Arie_Kusuma_A)\
**Post date:** [April 11, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/var-nil-if-false-p-var-nil-true-bug-of-the-day/134474/7 "2008-04-11T12:53:27Z")

</div>

On 11/04/2008, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Be careful when testing issues that involve local variables in IRB.  
> Top level local variables such as these do behave differently in IRB.

Oh I see, thanks for reminding Robert.

#!/usr/bin/ruby

# nil-check.rb

begin  
p var1  
rescue NameError =\> e  
puts e.message  
end

var1 = nil if false  
p var1

begin  
p var2  
rescue NameError =\> e  
puts e.message  
end

var2 = ‘nothing’ if false  
p var2

> Cheers
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

–  
blog: [http://tinyurl.com/2bjgvn](http://tinyurl.com/2bjgvn),  
ruby: [Tentang Ruby](http://www.ruby-lang.org/id/about)

“Developer Time is expensive…  
Servers are cheap…”, Ezra Z. - Merb
