# Instance and class variable assignment

**URL:** <https://rubytalk.org/t/instance-and-class-variable-assignment/21646>\
**Category:** ruby-talk\
**Created:** [18 October 2005 13:06 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646 "2005-10-18T13:06:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [18 October 2005 13:06 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646/1 "2005-10-18T13:06:57Z")

</div>

I know this has been suggested before, but the only reason for rejection i found at the RCR archive was a dead end link...

Can someone explain to me why this shouldn't be implemented in Ruby:

&nbsp;&nbsp;&nbsp;def foo(@a, @b = "foo", @@c = "bar")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;# should do the same as this  
&nbsp;&nbsp;&nbsp;def foo(a, b = "foo", c = "bar")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@a = a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@b = b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@c = c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
&nbsp;&nbsp;&nbsp;end

I personally think it looks very interesting, especially since most initialize methods (at least most of those I write) just set instance and class variables.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 13:16 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646/2 "2005-10-18T13:16:57Z")

</div>

Hrm, reminds me of an OptionParser trick that I like:

&nbsp;&nbsp;options = Hash.new  
&nbsp;&nbsp;opts = OptionParser.new do |opts|  
&nbsp;&nbsp;&nbsp;&nbsp;opts.on('-r', '--reload', 'Reloads something') { |options[:reload]|  
}  
&nbsp;&nbsp;&nbsp;&nbsp;opts.on('-q' ,'--quiet', 'Squelches messages') { |options[:quiet]|  
}  
&nbsp;&nbsp;end

That said, I think with method definitions I'd still avoid putting  
instance/class variables in the argument list.

---

<div class="post-metadata">

**Author:** ![Christophe\_Grandsire](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@Christophe\_Grandsire](https://rubytalk.org/u/Christophe_Grandsire)\
**Post date:** [18 October 2005 14:03 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646/3 "2005-10-18T14:03:29Z")

</div>

Selon Daniel Schierbeck \<daniel.schierbeck@gmail.com\>:

> Can someone explain to me why this shouldn't be implemented in Ruby:
> 
> &nbsp;&nbsp;&nbsp;def foo(@a, @b = "foo", @@c = "bar")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
> &nbsp;&nbsp;&nbsp;end

My eyes!!! \>\*\_\*\<

> &nbsp;&nbsp;&nbsp;# should do the same as this  
> &nbsp;&nbsp;&nbsp;def foo(a, b = "foo", c = "bar")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@a = a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@b = b  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@c = c  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
> &nbsp;&nbsp;&nbsp;end
> 
> I personally think it looks very interesting, especially since most  
> initialize methods (at least most of those I write) just set instance  
> and class variables.

I can see why you're interested (it shortens things quite a bit), but I think  
it's a wrong idea. It's confusing to have method arguments that are not  
method-local. Isn't that why the current behaviour of block arguments is going  
to be changed? 🙂

> **···**
>
> --  
> Christophe Grandsire.
> 
> [http://rainbow.conlang.free.fr](http://rainbow.conlang.free.fr)
> 
> It takes a straight mind to create a twisted conlang.

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [18 October 2005 14:30 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646/4 "2005-10-18T14:30:24Z")

</div>

> Selon Daniel Schierbeck \<daniel.schierbeck@gmail.com\>:  
> \> Can someone explain to me why this shouldn't be implemented in Ruby:  
> \>  
> \> def foo(@a, @b = "foo", @@c = "bar")  
> \> # do something  
> \> end

Yes. Matz doesn't like it. 😉 (And I agree with Christophe -- my eyes! 😉

-austin

> **···**
>
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Bob\_Hutchison1](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@Bob\_Hutchison1](https://rubytalk.org/u/Bob_Hutchison1)\
**Post date:** [18 October 2005 15:29 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646/5 "2005-10-18T15:29:18Z")

</div>

And what are the binding rules?

def foo(@a = 1, @@b = @a)

is equivalent to what? Common Lisp has two binding rules that generally used, e.g. let and let\*. These would give you:

def foo(a, b)  
&nbsp;&nbsp;&nbsp;old\_a = @a  
&nbsp;&nbsp;&nbsp;@a = a  
&nbsp;&nbsp;&nbsp;@@b = old\_a  
end

and

def foo(x, a, b)  
&nbsp;&nbsp;&nbsp;@a = a  
&nbsp;&nbsp;&nbsp;@@b = @a  
end

You need both in CL.

Cheers,  
Bob

> **···**
>
> On Oct 18, 2005, at 10:03 AM, Christophe Grandsire wrote:
> 
> > Selon Daniel Schierbeck \<daniel.schierbeck@gmail.com\>:
> > 
> > > Can someone explain to me why this shouldn't be implemented in Ruby:
> > > 
> > > &nbsp;&nbsp;&nbsp;def foo(@a, @b = "foo", @@c = "bar")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
> > > &nbsp;&nbsp;&nbsp;end
> > 
> > My eyes!!! \>\*\_\*\<
> 
> ----  
> Bob Hutchison -- blogs at \<[http://www.recursive.ca/hutch/&gt](http://www.recursive.ca/hutch/&gt);  
> Recursive Design Inc. -- \<[http://www.recursive.ca/&gt](http://www.recursive.ca/&gt);  
> Raconteur -- \<[http://www.raconteur.info/&gt](http://www.raconteur.info/&gt);

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [18 October 2005 14:41 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646/6 "2005-10-18T14:41:58Z")

</div>

Austin Ziegler wrote:

> > Selon Daniel Schierbeck \<daniel.schierbeck@gmail.com\>:
> > 
> > > Can someone explain to me why this shouldn't be implemented in Ruby:
> > > 
> > > &nbsp;&nbsp;def foo(@a, @b = "foo", @@c = "bar")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;# do something  
> > > &nbsp;&nbsp;end
> 
> Yes. Matz doesn't like it. 😉 (And I agree with Christophe -- my eyes! 😉
> 
> -austin  
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

Bollocks ☹

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [18 October 2005 14:54 UTC](https://rubytalk.org/t/instance-and-class-variable-assignment/21646/7 "2005-10-18T14:54:29Z")

</div>

It's a bit of an oversimplification to simply say that Matz doesn't  
like, but I do understand why. It isn't exactly intuitive to set  
instance variables in a method signature. (I'm not invoking POLS here,  
I'm suggesting instead that it would be unnecessarily confusing.) That  
said, it might be nice to have a language construct like C++'s  
initialization list:

&nbsp;&nbsp;class Person  
&nbsp;&nbsp;{  
&nbsp;&nbsp;Person() : name(""), age(-1)  
&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;// The rest of Person's default constructor here.  
&nbsp;&nbsp;}  
&nbsp;&nbsp;}

-austin

> **···**
>
> On 10/18/05, Daniel Schierbeck \<daniel.schierbeck@gmail.com\> wrote:
> 
> > Austin Ziegler wrote:
> > 
> > > > Selon Daniel Schierbeck \<daniel.schierbeck@gmail.com\>:
> > > > 
> > > > > Can someone explain to me why this shouldn't be implemented in Ruby:  
> > > > > &nbsp;&nbsp;def foo(@a, @b = "foo", @@c = "bar")  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;# do something  
> > > > > &nbsp;&nbsp;end
> > > 
> > > Yes. Matz doesn't like it. 😉 (And I agree with Christophe -- my  
> > > eyes! 😉
> > 
> > Bollocks ☹
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca
