Feature #12578
closedInstance Variables Assigned In parameters ( ala Crystal? )
Description
When I first saw the instance assignments in Crystal, I was floored... I have looked at all 18 pages in this tracker and thought it "must" have been mentioned in here... but I don't see it... pardon me if I missed it...
BUT
so in Crystal, this :
class MyStuff
def initialize @thing_one , @thing_two
end
end
would replace :
class MyStuff
def initialize thing_one , thing_two
@thing_one = thing_one
@thing_two = thing_two
end
end
One of the things I loved about ruby was attr_accessor
/ reader
/ writer
...
I know in Crystal, it is a macro, but still at the millions of lines of boilerplate that would go away...
Has no one thought of stealing this from Crystal or is there a good reason this does not exist? ( and I will go sit down and color now )
Updated by duerst (Martin Dürst) over 8 years ago
I personally think this syntax looks quite good. I wonder whether it should be limited to initialize or not.
Matt Lankford wrote:
I have looked at all 18 pages in this tracker and thought it "must" have been mentioned in here... but I don't see it... pardon me if I missed it...
Can you link some of these pages with "related issues"?
Updated by phluid61 (Matthew Kerwin) over 8 years ago
Here is some history of this proposal:
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
- Is duplicate of Feature #5825: Sweet instance var assignment in the object initializer added
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
- Is duplicate of Feature #8563: Instance variable arguments added
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
- Is duplicate of Feature #12023: Allow ivars to be used as method arguments added
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
- Description updated (diff)
Updated by shevegen (Robert A. Heiler) over 8 years ago
I like the idea behind it.
I am not sure about the syntax itself. Would this break existing code? What are the constraints how it is used?
Updated by lankford (Matt Lankford) over 8 years ago
Matthew Kerwin wrote:
Here is some history of this proposal:
You sir, are a much better "looker" than I ;)
well done!
Updated by matz (Yukihiro Matsumoto) over 8 years ago
- Status changed from Open to Feedback
Arguments are giving names to passed values, which is different from attribute (instance variables) initialization. I think they should be separated clearly. And for most of the cases, only initialize
needs this kind of initialization.
My idea is https://bugs.ruby-lang.org/issues/8563#note-3
Matz.
Updated by jwmittag (Jörg W Mittag) about 8 years ago
By the way, just as a historical note: up to and including Ruby 1.8.7, you could actually do this:
define_method(:initialize) {|@thing_one, @thing_two|}
I have never actually seen this, though, which you can take as an indication of the usefulness of your suggestion.
Updated by nobu (Nobuyoshi Nakada) about 8 years ago
- Has duplicate Feature #12820: Shorter syntax for assigning a method argument to an instance variable added
Updated by matz (Yukihiro Matsumoto) almost 7 years ago
- Status changed from Feedback to Rejected
My opinion has not been changed from https://bugs.ruby-lang.org/issues/8563#note-3
I am strongly against code like
def initialize(@foo, @bar)
end
I don't care about define_attr_initialize
not being "the solution".
Matz.