# Attribute setters for instance variables

**URL:** <https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661>\
**Category:** Ruby\
**Created:** [March 11, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661 "2006-03-11T18:19:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661/1 "2006-03-11T18:19:57Z")

</div>

Hello,

The Pickaxe book presents the following example:

class Incorrect  
attr\_accessor :one, :two  
def initialize  
one = 1  
self.two = 2  
end  
end

obj = Incorrect.new

p obj.one  
p obj.two

The first thing printed is ‘nil’ and not ‘1’ since “one = 1” assigns to  
a local variable of method initialize instead of the instance variable.  
So far it’s clear.  
Now, they propose a solution: to use self.one instead of just one.  
Won’t be using @one simpler and also correct ? What am I missing ?

Thanks

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661/2 "2006-03-11T18:26:11Z")

</div>

removed\_email\_address@domain.invalid wrote:

> end  
> Won’t be using @one simpler and also correct ? What am I missing ?
> 
> Thanks

@one = 1 is setting the instance variable itself, and that’s fine for  
accessors. If you want to do something special on =() then you will want  
to use the self.one style. e.g.

def Foo  
def one=(n)  
@one = n+42  
end  
def initialize(n)  
self.one = 0 # @one == 42  
@one = 0 # @one == 0  
end  
end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661/3 "2006-03-11T18:26:12Z")

</div>

Hans F. wrote:

> > end  
> > Now, they propose a solution: to use self.one instead of just one.  
> > def one=(n)  
> > @one = n+42  
> > end  
> > def initialize(n)  
> > self.one = 0 # @one == 42  
> > @one = 0 # @one == 0  
> > end  
> > end

Thanks for your swift and insightful reply.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661/4 "2006-03-11T21:12:15Z")

</div>

On Sun, 12 Mar 2006 removed\_email\_address@domain.invalid wrote:

> end  
> Won’t be using @one simpler and also correct ? What am I missing ?  
> def one= arg  
> log{ “somthing about one” }  
> validate{ arg }  
> notify\_observers :one, arg  
> @one = arg  
> end

just a few examples.

this become even more true when the number attribues increases since you  
can

class C  
ATTRIBUTES = %w[one two three four five six seven]  
def initialize kws = {}  
kws.each{|kw, val| send “#{ kw }=”, val}  
end  
end

in one line even if the number of attributes is huge.

i’d also argue that the simplest thing is to simply make the getter take  
and  
optional argument so one can do

def initialize  
one 1  
two 2  
end

but that’s a matter of taste.

hth - cheers.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2006, 5:10am UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661/5 "2006-03-12T05:10:59Z")

</div>

Mike A. wrote:

> > > ```
> > > self.two = 2
> > > 
> > > ```
> > > 
> > > So far it’s clear.  
> > > def Foo  
> > > advantages to this, including fix of the problem above, hard to debug  
> > > spelling mistakes, clearer code, etc. A simple ‘var’ declaration would  
> > > be simple:

What problem? I don’t see any problem, I quite like that behavior. I  
also like not declaring variables, the inconvenience from spelling  
errors is minimal, especially when the telltale nil problems are almost  
as useful as a compiler error most of the time.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2006, 2:26am UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661/6 "2006-03-12T02:26:38Z")

</div>

Hans F. wrote:

> > end  
> > Now, they propose a solution: to use self.one instead of just one.  
> > def one=(n)  
> > @one = n+42  
> > end  
> > def initialize(n)  
> > self.one = 0 # @one == 42  
> > @one = 0 # @one == 0  
> > end  
> > end

I really wish Ruby required explicit locals declaration. There are many  
advantages to this, including fix of the problem above, hard to debug  
spelling  
mistakes, clearer code, etc. A simple ‘var’ declaration would be  
simple:

def Foo  
def one=(n)  
@one = n+42  
end  
def initialize(n)  
var x, y = n \* n, 1 / n  
one = 0 # @one == 42  
@one = 0 # @one == 0  
z = 10 # error  
end  
end

Regards,  
Mike

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2006, 7:05am UTC](https://www.ruby-forum.com/t/attribute-setters-for-instance-variables/54661/7 "2006-03-12T07:05:35Z")

</div>

Hans F. wrote: [full message below]

> > I really wish Ruby required explicit locals declaration. There are  
> > many advantages to this, including fix of the problem above, hard to  
> > debug spelling mistakes, clearer code, etc. A simple ‘var’  
> > declaration would be simple:
> 
> What problem? I don’t see any problem, I quite like that behavior. I  
> also like not declaring variables, the inconvenience from spelling  
> errors is minimal, especially when the telltale nil problems are  
> almost  
> as useful as a compiler error most of the time.

I can’t imagine coding thousands of lines of code without variable  
declarations. If you catch the nil errors caused by spelling mistakes  
right  
away, sure, you can easily track it down. But I’d rather have the  
compiler  
tell me that a variable doesn’t exist than to give me an nil error who  
knows  
where. ‘var’ is only 4 more letters, and I’m not saying limit them to  
the  
beginning of the method or block. The only change would be prepending  
‘var’.

Mike
