— 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^^
###############
— test1.rb —
b = 1
puts a if a = b
— test2.rb —
a = 1 if false
puts defined?(a)
###############
###############
On Aug 18, 2008, at 8:29, Kyung won Cheon wrote:
— test1.rb —
b = 1
puts a if a = bNameError: 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
Hier from Programming Ruby book. Its not exactly for this case, but i
think it gets close :
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.
Greets
On Mon, 18 Aug 2008 15:29:04 +0900
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs