Project

General

Profile

Actions

Feature #12820

closed

Shorter syntax for assigning a method argument to an instance variable

Added by fornellas (Fabio Pugliese Ornellas) over 7 years ago. Updated over 6 years ago.

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

Description

Hello,

The pattern:

class Person
  def initialize(name:)
    @name = name
    @age  = 0
  end
end

is so common, that Cristal Programming Language has a shortcut for it:
https://crystal-lang.org/docs/syntax_and_semantics/methods_and_instance_variables.html

class Person
  def initialize(@name : String)
    @age = 0
  end
end

This is a feature request to implement the same style to Ruby syntax, supporting both isolated parameters and Hash style parameters. Eg:

class Person
  attr_reader :name, :age
  def initialize(@name, @age:)
  end
end
fabio = Person.new("Fabio", age: 34)
fabio.name #=> "Fabio"
fabio.age  #=> 34

This will be very useful in methods that accept a lot of parameters and certainly will avoid bugs due to decreased duplicated code (eg: addind a new parameter to a method, and forgetting to set the instance variable to it). Other than implementing support for the syntax itself, I believe RDoc would also have to be updated, not to expose the "@" on documentation (as it is not relevant there).

What do you think?

Thank you.


Related issues 5 (3 open2 closed)

Related to Ruby master - Feature #15192: Introduce a new "shortcut assigning" syntax to convenient setup instance variablesOpenmatz (Yukihiro Matsumoto)Actions
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
Is duplicate of Ruby master - Feature #12578: Instance Variables Assigned In parameters ( ala Crystal? )RejectedActions

Updated by phluid61 (Matthew Kerwin) over 7 years ago

See:

  • #12578 Instance Variables Assigned In parameters ( ala Crystal? )
  • #5825 Sweet instance var assignment in the object initializer
  • #8563 Instance variable arguments
  • #12023 Allow ivars to be used as method arguments

Is this now the most-duplicated feature request in Ruby?

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

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

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

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

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Is duplicate of Feature #12023: Allow ivars to be used as method arguments added
Actions #5

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Is duplicate of Feature #12578: Instance Variables Assigned In parameters ( ala Crystal? ) added

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Description updated (diff)

Matthew Kerwin wrote:

Is this now the most-duplicated feature request in Ruby?

By crystal?

Actions #7

Updated by matz (Yukihiro Matsumoto) over 6 years ago

  • Status changed from Open 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 #8

Updated by matz (Yukihiro Matsumoto) over 5 years ago

  • Related to Feature #15192: Introduce a new "shortcut assigning" syntax to convenient setup instance variables added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0