# Syntax suggestion

**URL:** <https://www.ruby-forum.com/t/syntax-suggestion/106183>\
**Category:** Ruby\
**Created:** [June 7, 2007, 7:46am UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183 "2007-06-07T07:46:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jbc](https://www.ruby-forum.com/u/jbc)\
**Post date:** [June 7, 2007, 7:46am UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183/1 "2007-06-07T07:46:20Z")

</div>

(I posted this before on the google-group rather than the usenet  
group, but it seems not to have shown up. I don’t know why. If this is  
a duplicate for anyone, my apologies)

def initialize(foo,bar,bat)  
@foo,@bar,@bat = foo,bar,bat  
end

Seems clumsy and not at all DRY.

What would seem the obvious approach to me would be this:

def initialize(@foo,@bar,@bat)  
end

Is there a reason it’s not done that way?

---

<div class="post-metadata">

**Author:** ![jbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jbc](https://www.ruby-forum.com/u/jbc)\
**Post date:** [June 7, 2007, 8:30am UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183/2 "2007-06-07T08:30:24Z")

</div>

jbc wrote:

> What would seem the obvious approach to me would be this:
> 
> def initialize(@foo,@bar,@bat)  
> end
> 
> Is there a reason it’s not done that way?

This has been discussed on ruby-talk, but it might have been a few years  
ago.

You can do this:

class Foo  
define\_method :initialize do |@x,@y,@z| end  
end

p Foo.new(1,2,3) # ==\> #\<Foo:0xb7cce1e4 @x=1, @z=3, @y=2\>

You might want to verify that this construct is allowed in 1.9 before  
using it heavily. ISTR it is deprecated.

If a method has more than two positional arguments, I tend to look for  
alternatives. YMMV of course.

---

<div class="post-metadata">

**Author:** ![jbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jbc](https://www.ruby-forum.com/u/jbc)\
**Post date:** [June 7, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183/3 "2007-06-07T12:51:00Z")

</div>

Hi –

On Thu, 7 Jun 2007, Joel VanderWerf wrote:

> > What would seem the obvious approach to me would be this:
> > 
> > def initialize(@foo,@bar,@bat)  
> > end
> > 
> > Is there a reason it’s not done that way?
> 
> This has been discussed on ruby-talk, but it might have been a few years ago.

It’s also a rejected RCR:  
[http://oldrcrs.rubypal.com/rejected.html#rcr3](http://oldrcrs.rubypal.com/rejected.html#rcr3)

David

---

<div class="post-metadata">

**Author:** ![jbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jbc](https://www.ruby-forum.com/u/jbc)\
**Post date:** [June 7, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183/4 "2007-06-07T17:10:46Z")

</div>

On Jun 7, 12:29 am, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Foo  
> define\_method :initialize do |@x,@y,@z| end  
> end
> 
> p Foo.new(1,2,3) # ==\> #\<Foo:0xb7cce1e4 @x=1, @z=3, @y=2\>
> 
> You might want to verify that this construct is allowed in 1.9 before  
> using it heavily. ISTR it is deprecated.

Can someone confirm or deny that this is deprecated in 1.9?

Not just the specific case above, but the general functionality that  
block parameters can be instance variables, setting the instance  
variable as a side-effect of just invoking the block. For example:

p @a  
#=\> nil

3.times{ |@a| }

p @a  
#=\> 2

p VERSION  
#=\> “1.8.5”

---

<div class="post-metadata">

**Author:** ![jbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jbc](https://www.ruby-forum.com/u/jbc)\
**Post date:** [June 7, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183/5 "2007-06-07T17:36:50Z")

</div>

On 6/7/07, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can someone confirm or deny that this is deprecated in 1.9?  
> I remember Matz saying some times ago that it is deprecated. I did not  
> post this before b/c I just cannot come up with a URL ☹

But I remember distinctively as I really liked the syntax :((

Robert

---

<div class="post-metadata">

**Author:** ![jbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jbc](https://www.ruby-forum.com/u/jbc)\
**Post date:** [June 7, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183/6 "2007-06-07T18:00:54Z")

</div>

Phrogz wrote:

> Can someone confirm or deny that this is deprecated in 1.9?  
> yep, deprecated:

$ ruby1.9 -e ’  
class Foo  
define\_method(:initialize) do |@x,@y,@z| end  
end’  
formal argument cannot be an instance variable  
define\_method(:initialize) do |@x,@y,@z| end

Daniel

---

<div class="post-metadata">

**Author:** ![jbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jbc](https://www.ruby-forum.com/u/jbc)\
**Post date:** [June 7, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/syntax-suggestion/106183/7 "2007-06-07T18:11:56Z")

</div>

On 07.06.2007 07:42, jbc wrote:

> What would seem the obvious approach to me would be this:
> 
> def initialize(@foo,@bar,@bat)  
> end
> 
> Is there a reason it’s not done that way?

See the other replies for explanations. I want to show an alternative:

Foo = Struct.new :foo, :bar, :bat

f=Foo.new 1,2,3

You’ll also get comparison logic and hashing for free. And you can even  
define methods on the class directly like this:

Foo = Struct.new :foo, :bar, :bat do  
def size  
foo + bar + bat  
end  
end

I use that pretty often because it saves even more typing than just the  
initialize code. 🙂

Kind regards

robert
