Project

General

Profile

Actions

Feature #12578

closed

Instance Variables Assigned In parameters ( ala Crystal? )

Added by lankford (Matt Lankford) over 8 years ago. Updated about 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:76329]

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 )


Related issues 4 (2 open2 closed)

Is duplicate of Ruby master - Feature #5825: Sweet instance var assignment in the object initializerAssignedmatz (Yukihiro Matsumoto)Actions
Is duplicate of Ruby master - Feature #8563: Instance variable argumentsRejectedmatz (Yukihiro Matsumoto)Actions
Is duplicate of Ruby master - Feature #12023: Allow ivars to be used as method argumentsOpenActions
Has duplicate Ruby master - Feature #12820: Shorter syntax for assigning a method argument to an instance variableRejectedActions
Actions #1

Updated by hsbt (Hiroshi SHIBATA) over 8 years ago

  • Assignee deleted (core)

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"?

Actions #4

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Is duplicate of Feature #5825: Sweet instance var assignment in the object initializer added
Actions #5

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Is duplicate of Feature #8563: Instance variable arguments added
Actions #6

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) over 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.

Actions #12

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
Actions #13

Updated by matz (Yukihiro Matsumoto) about 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0