Project

General

Profile

Actions

Feature #5825

open

Sweet instance var assignment in the object initializer

Added by goshakkk (Gosha Arinich) about 12 years ago. Updated 6 months ago.

Status:
Assigned
Target version:
-
[ruby-core:<unknown>]

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?


Related issues 7 (4 open3 closed)

Related to Ruby master - Feature #17942: Add a `initialize(public @a, private @b)` shortcut syntax for defining public/private accessors for instance vars as part of constructorOpenActions
Has duplicate Ruby master - Feature #8563: Instance variable argumentsRejectedmatz (Yukihiro Matsumoto)Actions
Has duplicate Ruby master - Feature #12023: Allow ivars to be used as method argumentsOpenActions
Has duplicate Ruby master - Feature #12578: Instance Variables Assigned In parameters ( ala Crystal? )RejectedActions
Has duplicate Ruby master - Feature #12820: Shorter syntax for assigning a method argument to an instance variableRejectedActions
Has duplicate Ruby master - Feature #15192: Introduce a new "shortcut assigning" syntax to convenient setup instance variablesOpenmatz (Yukihiro Matsumoto)Actions
Has duplicate Ruby master - Feature #19898: Special syntax for instance variable assignment OpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0