# What's difference?

**URL:** <https://www.ruby-forum.com/t/whats-difference/146121>\
**Category:** Ruby\
**Created:** [August 18, 2008, 8:32am UTC](https://www.ruby-forum.com/t/whats-difference/146121 "2008-08-18T08:32:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [August 18, 2008, 8:32am UTC](https://www.ruby-forum.com/t/whats-difference/146121/1 "2008-08-18T08:32:21Z")

</div>

— test1.rb —

b = 1  
puts a if a = b

# NameError: undefined local variable or method `a’ for main:Object

— test2.rb —

a = 1 if false  
puts defined?(a)

# local-variable

###############

# Help Me^^

###############

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [August 18, 2008, 9:10am UTC](https://www.ruby-forum.com/t/whats-difference/146121/2 "2008-08-18T09:10:40Z")

</div>

On Aug 18, 2008, at 8:29, Kyung won Cheon wrote:

> — test1.rb —
> 
> b = 1  
> puts a if a = b
> 
> # NameError: undefined local variable or method `a’ for main:Object

Parser peculiarity. You can’t use a local variable defined for the  
first time in the clause of an inline-if for that statement. Define a  
before it reaches that line, or use this instead:

if a = b then puts a end

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [August 18, 2008, 10:31am UTC](https://www.ruby-forum.com/t/whats-difference/146121/3 "2008-08-18T10:31:32Z")

</div>

Hier from Programming Ruby book. Its not exactly for this case, but i  
think it gets close :

* * *

## When Ruby sees a name such as ``a’’ in an expression, it needs to determine if it is a local variable reference or a call to a method with no parameters. To decide which is the case, Ruby uses a heuristic. As Ruby reads a source file, it keeps track of symbols that have been assigned to. It assumes that these symbols are variables. When it subsequently comes across a symbol that might be either a variable or a method call, it checks to see if it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable; otherwise it treats it as a method call.

There is also this example down below :

* * *

Note that the assignment does not have to be executed—Ruby just has to  
have seen it. This program does not raise an error.

## a = 1 if false; a

Greets

On Mon, 18 Aug 2008 15:29:04 +0900
