bug in ruby

S

sairam MP

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
 
D

dblack

Hi --

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

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)
 
M

Mike Cahill

U29ycnkgLSB3aGVyZSdzIHRoZSBidWc/ICBZb3UgdGhpbmsgdGhlDQoNCnAgYmFyDQoNCnNob3Vs
ZCBnaXZlIDE/ICBJIGJlbGlldmUgeW91IGFyZSB0aGlua2luZyBvZiANCg0KcCBAYmFyID8/DQoN
Ck9yIGhhdmUgSSAtIGFzIEkgb2Z0ZW4gZG8gLSBtaXNzZWQgdGhlIHBvaW50PyA9KQ0KICANCg0K
LS0tLS1PcmlnaW5hbCBNZXNzYWdlLS0tLS0NCkZyb206IHNhaXJhbSBNUCA8c2FpNDM4QGdtYWls
LmNvbT4NCkRhdGU6IFR1ZSwgMjIgTWF5IDIwMDcgMjE6NDE6MDMgDQpUbzpydWJ5LXRhbGtAcnVi
eS1sYW5nLm9yZyAocnVieS10YWxrIE1MKQ0KU3ViamVjdDogYnVnIGluIHJ1YnkNCg0KY2xhc3Mg
Rm9vDQogIGF0dHJfYWNjZXNzb3IgOmJhcg0KDQoNCiAgZGVmIGZvbw0KICAgIHNlbGYuYmFyID0g
MQ0KDQoNCiAgICBpZiBmYWxzZQ0KICAgICAgYmFyID0gMiAgICAgICAgIyBuZXZlciBleGVjdXRl
ZA0KICAgIGVuZA0KDQoNCiAgICBwIHNlbGYuYmFyICAgICAgICMgcHJpbnRzIDENCiAgICBwIGJh
ciAgICAgICAgICAgICMgcHJpbnRzIG5pbA0KICBlbmQNCmVuZA0KDQotLSANClBvc3RlZCB2aWEg
aHR0cDovL3d3dy5ydWJ5LWZvcnVtLmNvbS8uDQoNCg==
 
P

Peter Hickman

sairam said:
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.
 
B

Brian Candler

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

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.
 
R

Robert Klemme

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.

Additional reference material:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,259
Messages
2,571,295
Members
47,931
Latest member
alibsskamoSeAve

Latest Threads

Top