# Foo || 0 will cause exception but not for himself

**URL:** <https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238>\
**Category:** Ruby\
**Created:** [September 22, 2007, 4:37am UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238 "2007-09-22T04:37:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 22, 2007, 4:37am UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/1 "2007-09-22T04:37:37Z")

</div>

if foo is undefined, it will throw exception when referred to… as in  
try 1, 2, and 3 below. However, it won’t throw exception when assigned  
to himself… how is that interpreted?

irb(main):001:0\> foo  
NameError: undefined local variable or method `foo' for main:Object from (irb):1:in`Kernel#binding’

irb(main):002:0\> foo || 0  
NameError: undefined local variable or method `foo' for main:Object from (irb):2:in`Kernel#binding’

irb(main):003:0\> bar = foo || 0  
NameError: undefined local variable or method `foo' for main:Object from (irb):3:in`Kernel#binding’

irb(main):004:0\> foo = foo || 0  
=\> 0

irb(main):005:0\> foo  
=\> 0

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 22, 2007, 5:06am UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/2 "2007-09-22T05:06:20Z")

</div>

On Sep 21, 8:37 pm, SpringFlowers AutumnMoon  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> from (irb):2:in `Kernel#binding'
> 
> ```
> 
> irb(main):003:0\> bar = foo || 0  
> NameError: undefined local variable or method `foo' for main:Object from (irb):3:in `Kernel#binding’
> 
> irb(main):004:0\> foo = foo || 0  
> =\> 0
> 
> irb(main):005:0\> foo  
> =\> 0

When Ruby says “undefined local variable or method”, what it means is  
“Hey, I don’t know what this is, and I don’t know if it’s supposed to  
be a variable or a method.”

When you write “foo = …” Ruby says, “Ah ha! Foo is supposed to be a  
variable. I’ll treat it as one.”

That’s my (somewhat informed) guess anyhow. The determination of the  
method/variableness of ‘foo’ is determined before runtime, when the  
lexer/parser is analyzing the program. This is similar to why this  
code…  
if foo=42  
p foo  
end  
works just fine, but this code:  
p foo if foo=42  
throws an error. When it first see’s foo in the first code sample, it  
knows that it is a variable. When it first sees foo in the second code  
sample, it doesn’t know what it is yet, and throws an error to  
indicate its confusion.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 22, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/3 "2007-09-22T14:10:28Z")

</div>

SpringFlowers AutumnMoon wrote:

> if foo is undefined, it will throw exception when referred to… as in  
> try 1, 2, and 3 below. However, it won’t throw exception when assigned  
> to himself… how is that interpreted?
> 
> irb(main):004:0\> foo = foo || 0  
> =\> 0
> 
> irb(main):005:0\> foo  
> =\> 0

Maybe your Variable/Method Ambiguity thread applies here?

if false  
foo = 10  
end

puts foo #nil

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 22, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/4 "2007-09-22T15:26:34Z")

</div>

7stud – wrote:

> if false  
> foo = 10  
> end
> 
> puts foo #nil

i see…

so after

if false  
foo = 10  
end

now, bar = foo || 0 will run.

Since for instance methods, the setter method can be foo=()

so how about just a method not inside a class, can it be named “foo=” as  
well? if so, how does Ruby know foo=1 is really an assignment or a  
method call foo=(1)?

Ruby doesn’t seem to like “foo=” as a method:

def foo=(i)  
p “ha”, i+1  
end

def bar?(i)  
i % 10  
end

p foo=(1)

p foo

p bar?(22)

it will print out

1  
1  
2

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 22, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/5 "2007-09-22T15:56:20Z")

</div>

On Sep 22, 7:26 am, SpringFlowers AutumnMoon  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> so how about just a method not inside a class, can it be named “foo=” as  
> well? if so, how does Ruby know foo=1 is really an assignment or a  
> method call foo=(1)?
> 
> Ruby doesn’t seem to like “foo=” as a method:

self.foo = 10

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 22, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/6 "2007-09-22T22:51:45Z")

</div>

Gavin K. wrote:

> On Sep 22, 7:26 am, SpringFlowers AutumnMoon  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > so how about just a method not inside a class, can it be named “foo=” as  
> > well? if so, how does Ruby know foo=1 is really an assignment or a  
> > method call foo=(1)?
> > 
> > Ruby doesn’t seem to like “foo=” as a method:
> 
> self.foo = 10

def foo=(val)  
“hello”  
end

puts self.foo = 10 #10

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 24, 2007, 5:00am UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/7 "2007-09-24T05:00:36Z")

</div>

On Sep 22, 2:51 pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def foo=(val)  
> “hello”  
> end
> 
> puts self.foo = 10 #10

Er, what’s your point? Methods whose names end with a ‘=’ character  
always return the value ‘assigned’ to them, not the last expression in  
the method. How is that relevant to this discussion of how to invoke  
them, and the scope and determinatino of variables versus methods?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 24, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/foo-0-will-cause-exception-but-not-for-himself/115238/8 "2007-09-24T13:49:57Z")

</div>

On 9/23/07, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > self.foo = 10  
> > > them, and the scope and determinatino of variables versus methods?  
> > > Actually, it’s not that, it’s the way assignments get parsed/compiled:

irb(main):001:0\> class Foo  
irb(main):002:1\> def x  
irb(main):003:2\> @x  
irb(main):004:2\> end  
irb(main):005:1\> def x=(val)  
irb(main):006:2\> @x = val  
irb(main):007:2\> “Surprise”  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> f = Foo.new  
=\> #Foo:0x7a1fc  
irb(main):011:0\> f.x=2  
=\> 2  
irb(main):012:0\> f. **send** (:x=, 3)  
=\> “Surprise”  
irb(main):013:0\> f.x  
=\> 3

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
