class Foo
attr_accessor :bar
def foo
self.bar = 1
if false
bar = 2 # never executed
end
p self.bar # prints 1
p bar # prints nil
end
end
···
--
Posted via http://www.ruby-forum.com/.
class Foo
attr_accessor :bar
def foo
self.bar = 1
if false
bar = 2 # never executed
end
p self.bar # prints 1
p bar # prints nil
end
end
--
Posted via http://www.ruby-forum.com/.
Hi --
On Tue, 22 May 2007, sairam MP wrote:
class Foo
attr_accessor :bardef foo
self.bar = 1if false
bar = 2 # never executed
endp self.bar # prints 1
p bar # prints nil
end
end
That's not a bug. The parser sees:
bar = 2
and that triggers the allocation of bar. Since the expression is
inside of the if false block, it never gets executed, so bar is nil.
David
--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
Sorry - where's the bug? You think the
p bar
should give 1? I believe you are thinking of
p @bar ??
Or have I - as I often do - missed the point? =)
-----Original Message-----
From: sairam MP <sai438@gmail.com>
Date: Tue, 22 May 2007 21:41:03
To:ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: bug in ruby
class Foo
attr_accessor :bar
def foo
self.bar = 1
if false
bar = 2 # never executed
end
p self.bar # prints 1
p bar # prints nil
end
end
--
Posted via http://www.ruby-forum.com/.
sairam MP wrote:
if false
bar = 2 # never executed
end
There is no way that this conditional will be triggered.
if expr
...
end
will only execute the body of the if statement if expr is true. False is never true (outside of politics), hence it is never executed.
This is a bug in your understanding of the language, not in the language
itself. You've not said exactly what you think it *should* do instead of
what it does, so it's hard to give an answer tailored to improving your
understandly.
But briefly: an assignment like "bar = 2" is seen at the time the program is
*parsed* and means that an unqualified "bar" is treated as a local variable
from that point onwards until the end of that scope. Whether or not it is
actually *executed* makes no difference.
When you write "self.bar" or "self.bar=" you are explicitly making a method
call to a method "bar" or "bar=" on the current object; this is never
treated as a local variable.
If you write "bar" by itself, this is treated as a method call on the
current object *unless* an assignment of the form "bar = x" has been seen by
the parser earlier in the scope.
Regards,
Brian.
On Tue, May 22, 2007 at 09:41:03PM +0900, sairam MP wrote:
class Foo
attr_accessor :bardef foo
self.bar = 1if false
bar = 2 # never executed
endp self.bar # prints 1
p bar # prints nil
end
end
____________________________________________________________________________________Luggage? GPS? Comic books?
Check out fitting gifts for grads at Yahoo! Search
http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz
--- sairam MP <sai438@gmail.com> wrote:
class Foo
attr_accessor :bardef foo
self.bar = 1if false
bar = 2 # never executed
endp self.bar # prints 1
p bar # prints nil
end
end--
Posted via http://www.ruby-forum.com/\.
[ snip ]
A gentle suggestion:
"Don't claim that you have found a bug"
http://www.catb.org/~esr/faqs/smart-questions.html#id270918
On 5/22/07, sairam MP <sai438@gmail.com> wrote:
Additional reference material:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO
Kind regards
robert
On 22.05.2007 14:53, Brian Candler wrote:
On Tue, May 22, 2007 at 09:41:03PM +0900, sairam MP wrote:
class Foo
attr_accessor :bardef foo
self.bar = 1if false
bar = 2 # never executed
endp self.bar # prints 1
p bar # prints nil
end
endThis is a bug in your understanding of the language, not in the language
itself. You've not said exactly what you think it *should* do instead of
what it does, so it's hard to give an answer tailored to improving your
understandly.But briefly: an assignment like "bar = 2" is seen at the time the program is
*parsed* and means that an unqualified "bar" is treated as a local variable
from that point onwards until the end of that scope. Whether or not it is
actually *executed* makes no difference.When you write "self.bar" or "self.bar=" you are explicitly making a method
call to a method "bar" or "bar=" on the current object; this is never
treated as a local variable.If you write "bar" by itself, this is treated as a method call on the
current object *unless* an assignment of the form "bar = x" has been seen by
the parser earlier in the scope.
Robert Klemme schrieb:
On 22.05.2007 14:53, Brian Candler wrote:
On Tue, May 22, 2007 at 09:41:03PM +0900, sairam MP wrote:
class Foo
...
if false
bar = 2 # never executed
end
...
p bar # prints nil
end
endBut briefly: an assignment like "bar = 2" is seen at the time the program is *parsed* and means that an unqualified "bar" is treated as a local variable from that point onwards until the end of that scope. Whether or not it is
scope. Whether or not it is actually *executed* makes no difference.Additional reference material:
It should be added in the documentation, that such an uninitialized local variable is set to the initial value "nil".
Wolfgang Nádasi-Donner