What's difference?

--- 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^^
###############
--
Posted via http://www.ruby-forum.com/.

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

···

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

--
# Mikael Høilund
def method_missing(m, a=0) a +
m.to_s[/[a-z]+/].size * 2; end
p What is the meaning of life?

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 Kyung won Cheon <kdream95@gmerce.co.kr> wrote:

--- 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^^
###############