# Bug in ruby

**URL:** <https://www.ruby-forum.com/t/bug-in-ruby/104784>\
**Category:** Ruby\
**Created:** [May 22, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784 "2007-05-22T14:41:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/1 "2007-05-22T14:41:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/2 "2007-05-22T14:45:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/3 "2007-05-22T14:49:47Z")

</div>

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? =)

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/4 "2007-05-22T14:50:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/5 "2007-05-22T14:54:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/6 "2007-05-22T14:55:49Z")

</div>

— sairam MP [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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=graduation+gifts&cs=bz](http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz)

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/7 "2007-05-22T15:07:51Z")

</div>

On 5/22/07, sairam MP [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

[snip]

A gentle suggestion:

“Don’t claim that you have found a bug”

[http://www.catb.org/~esr/faqs/smart-questions.html#id270918](http://www.catb.org/~esr/faqs/smart-questions.html#id270918)

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/8 "2007-05-22T15:00:50Z")

</div>

On 22.05.2007 14:53, Brian C. 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/language.html#UO](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![sai438](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sai438](https://www.ruby-forum.com/u/sai438)\
**Post date:** [May 22, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/bug-in-ruby/104784/9 "2007-05-22T15:36:05Z")

</div>

Robert K. 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:
> 
> [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO)

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

Wolfgang Nádasi-Donner
