Forum: Ruby-core Sweet instance var assignment in the object initializer

Posted by sikachu (Prem Sichanugrist) (Guest)
on 2013-02-25 23:40
(Received via mailing list)
Issue #5825 has been updated by sikachu (Prem Sichanugrist).


I think this is a good feature, so I'd like to support this (and 
possibly, provide a patch for this)

Reading from all the comments, I saw that someone has some concern about 
having this feature on another method definition (not just initialize) 
as well. I think we should implement it for any type of method 
definition if that's going to make the code cleaner, but the main focus 
here is for the initialize method.

As per comment 12, I think it's OK to start up with `def 
initialize(@foo, @bar, @baz)` and then refactor it if you need to 
perform any method on those arguments before store it to an instance 
variable.

I think adding support for only local and instance variables make sense, 
and I think we need to make sure that we're not supporting global 
variable like in comment 15.

Anyway, the benefit I'm seeing here is that we're not cluttering the 
initializer with all those obvious instance variable assignments. I also 
think it's a good syntactic sugar and it make sense after I saw all the 
usage from CoffeeScript. I hope I can help to make this happen in the 
next minor.
----------------------------------------
Feature #5825: Sweet instance var assignment in the object initializer
https://bugs.ruby-lang.org/issues/5825#change-37008

Author: goshakkk (Gosha Arinich)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor


I'm very excited about this feature in CoffeeScript, and think it might 
be a nice-to-have thing in Ruby 2.0.

That's how I think it would look like:

`class Me
  def initialize(@name, @age, @location); end
end`

So we can declare @variables in the initializer method parameters 
definition to avoid assigning instance variables from method arguments 
by hand, like:

`class Me
  def initialize(name, age, location)
    @name = name
    @age = age
    @location = location
  end
end`

Want to hear what do you guys think, does that feature worth being 
included in 2.0?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.