# \[Newbie\] Question about instance method scoping/parsing (?)

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-instance-method-scoping-parsing/81180>\
**Category:** Ruby\
**Created:** [January 5, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/newbie-question-about-instance-method-scoping-parsing/81180 "2007-01-05T23:37:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 5, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/newbie-question-about-instance-method-scoping-parsing/81180/1 "2007-01-05T23:37:55Z")

</div>

Can anyone explain to me what’s going on here?

* * *

#!/usr/bin/env ruby -w

class Thingy  
attr\_accessor :name

def test  
puts “self.name: #{self.name}”  
puts “name: #{name}”

```
name ||= 'default value'

puts "self.name: #{self.name}"
puts "name: #{name}"

```

end  
end

## thing1 = Thingy.new thing1.name = ‘custom value’ thing1.test

## $ ruby dditest.rb self.name: custom value name: custom value self.name: custom value name: default value

I thought calling a method without a receiver (i.e. name, name=) within  
an instance method would always call that method on the current object.  
Is this not true for setters (i.e. Thingy#name=)?

Additionally, why is the first instance of name interpreted as a method  
call and the last instance interpreted as (I think) a local variable?

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/newbie-question-about-instance-method-scoping-parsing/81180/2 "2007-01-06T19:30:20Z")

</div>

Hi –

On Sat, 6 Jan 2007, Chris G. wrote:

> I thought calling a method without a receiver (i.e. name, name=) within  
> an instance method would always call that method on the current object.  
> Is this not true for setters (i.e. Thingy#name=)?

It is not true for setters. The way it works is that when the parser  
sees:

bareword = …

it defines bareword as a local variable. Therefore, you always need  
an explicit receiver for that kind of method, so Ruby will know it’s a  
method.

There are some interesting side-effects from this. For example:

if false  
a = 1  
end

puts a # nil

Even though “a = 1” is never executed, it’s picked up by the parser,  
and a gets defined. That’s why “puts a” doesn’t raise an “unknown  
method or local variable” error.

> Additionally, why is the first instance of name interpreted as a method  
> call and the last instance interpreted as (I think) a local variable?

If both are defined, the local variable takes precedence. You can  
force method interpretation with parentheses:

name()

but of course in practice it’s better not to reuse the names.

David
