Forum: Ruby bug in ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0db083e56e4d69540df2ec1b00f40582?d=identicon&s=25 sairam MP (sai438)
on 2007-05-22 14:41
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-05-22 14:45
(Received via mailing list)
Hi --

On Tue, 22 May 2007, sairam MP wrote:

>    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
B0f5d09953d14dcc7705f928d503c824?d=identicon&s=25 Mike Cahill (Guest)
on 2007-05-22 14:49
(Received via mailing list)
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? =)
6d9bf78ca49a017e9e3e6b0357b6c59e?d=identicon&s=25 Peter Hickman (Guest)
on 2007-05-22 14:50
(Received via mailing list)
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-22 14:54
(Received via mailing list)
On Tue, May 22, 2007 at 09:41:03PM +0900, sairam MP wrote:
>     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.
D54eebeff364b2ab630294885fa902f1?d=identicon&s=25 Steven Burnett (Guest)
on 2007-05-22 14:55
(Received via mailing list)
--- sairam MP <sai438@gmail.com> wrote:

>     end
>
____________________________________________________________________________________Luggage?
GPS? Comic books?
Check out fitting gifts for grads at Yahoo! Search
http://search.yahoo.com/search?fr=oni_on_mail&p=gr...
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-05-22 15:00
(Received via mailing list)
On 22.05.2007 14:53, Brian Candler wrote:
>>       bar = 2        # never executed
> what it does, so it's hard to give an answer tailored to improving your
>
> 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/...

Kind regards

  robert
813f535246722b7bf02aacc9ce818de8?d=identicon&s=25 Bob Showalter (Guest)
on 2007-05-22 15:07
(Received via mailing list)
On 5/22/07, sairam MP <sai438@gmail.com> wrote:

[ snip ]

A gentle suggestion:

"Don't claim that you have found a bug"

http://www.catb.org/~esr/faqs/smart-questions.html#id270918
A131b672fdbd2a58dce12031ad78b121?d=identicon&s=25 Wolfgang Nádasi-Donner (wonado)
on 2007-05-22 15:36
(Received via mailing list)
Robert Klemme schrieb:
>>> end
>> 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
>> scope. Whether or not it is actually *executed* makes no difference.
> Additional reference material:
>
> http://www.ruby-doc.org/docs/ProgrammingRuby/html/...

It should be added in the documentation, that such an uninitialized
local
variable is set to the initial value "nil".

Wolfgang Nádasi-Donner
This topic is locked and can not be replied to.