Feature #5825
Sweet instance var assignment in the object initializer
| Status: | Assigned | Start date: | ||
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | - | |||
| Target version: | 2.0.0 |
Description
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?
History
Updated by lisovskyvlad (Vlad Lisosvky) 5 months ago
I like it. No stupid assigns.
Updated by rue (Eero Saynatkari) 5 months ago
Would be nice, and should be able to coexist with normal parameters:
def foo(bar, @baz, quux = @moomin)
…
end
And so on. What about splat- and block arguments? It gets a little ugly:
def foo(bar, *@baz, &@quux)
…
end
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) 5 months ago
+1 - too common use case
Updated by shyouhei (Shyouhei Urabe) 5 months ago
I liked this 1.8-specific trick:
define_method(:intialize){|@foo, @bar|}
but it was abondoned for any reason.
Updated by andhapp (Anuj Dutta) 5 months ago
+1. Common use case.
Updated by alexeymuranov (Alexey Muranov) 5 months ago
+1, why only initialize?
Updated by Eregon (Benoit Daloze) 5 months ago
I think most of the time you need to parse or check your arguments, in which case this syntax would not be practical.
Otherwise, you could use Struct to avoid the duplication:
class Me < Struct.new(:name, :age, :location)
end
Updated by goshakkk (Gosha Arinich) 5 months ago
Alexey Muranov wrote:
> +1, why only initialize?
It's just too common case. It could work for other methods as well.
Updated by shevegen (markus heiler) 5 months ago
Is this even possible with the Parser?
It would eliminate a few lines of code.
Also, I am not sure if this violates the principle of least matz surprise.
If I initially see
def initialize(@name, @age, @location)
I wonder a bit, because it does not feel consistent.
Perhaps it would be different if some kind of attr* could be
used.
attr_initialize :name, :age, :location
And then the above could work. Where the name would work just
similar to an attr_writer, but different in that it assumes
default values passed to initialize to automatically go towards
those instance variables (the order must be the same of course)
Updated by naruse (Yui NARUSE) 5 months ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Updated by ko1 (Koichi Sasada) 3 months ago
- Target version set to 2.0.0
if my memory serves me right, matz dislikes such a style of arguments.
Matz, could you explain the reason again?
I know some people like this style.
Updated by trans (Thomas Sawyer) 3 months ago
I think it's really not such a good idea. Often you'll just end up having to redo it anyway when you finally decide to coerce and/or validate arguments to make your code more robust, e,g,
def initialize(@name, @age, @location); end
Becomes
def initialize(name, age, location)
@name = name.to_s
@age = age.to_i
@location = Location.new(location)
end
Might as well write it out from the get-go in preparation.