Issue #8563 has been updated by marcandre (Marc-Andre Lafortune).


matz (Yukihiro Matsumoto) wrote:
> Arguments are giving names to passed values, which is different from attribute (instance variables) initialization. I think they should be separated clearly.

This may be a valid theoretical distinction, but what does this change in practice?

I feel there would be about no additional cognitive load if this was accepted. It is most probably the most requested feature

This is not surprising, since a majority of `initialize` with arguments is doing that!


> And for most of the cases, only `initialize` needs this kind of initialization.

Indeed. This doesn't change the case that a majority of `initialize` methods needs this kind of initialization.

Here's an example Rails app (which I didn't write): https://github.com/WikiEducationFoundation/WikiEduDashboard/search?utf8=%E2%9C%93&q=def+initialize&type=
A gem I'm using a lot these days: https://github.com/whitequark/parser/search?utf8=%E2%9C%93&q=def+initialize&type=

Since keyword arguments, I feel that's even more `true`, since `Struct` is no longer as useful.
 
> My idea is https://bugs.ruby-lang.org/issues/8563#note-3

With all due respect, this is not a good idea and it can't solve the issue. As was pointed out, there are many more cases that it doesn't handle:
- keyword arguments (that's absolutely major!)
- defaults
- dealing with other arguments
- doing extra code

I'd also mention the additional cognitive load involved.

Note that it has always been trivial to implement `define_attr_initialize` in pure Ruby. I've never seen such code though.

Take the top 100 gems, a few web apps, make an inventory of the `initialize` with parameters. I'm betting that the vast majority of these methods would be simplified by allowing instance variable arguments. I doubt that `define_attr_initialize` would help any but a small minority.


----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-67941

* Author: sawa (Tsuyoshi Sawada)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>