[#77789] [Ruby trunk Feature#12012] Add Boolean method — prodis@...
Issue #12012 has been updated by Fernando Hamasaki de Amorim.
4 messages
2016/10/27
[ruby-core:77522] [Ruby trunk Feature#12820] Shorter syntax for assigning a method argument to an instance variable
From:
matthew@...
Date:
2016-10-07 21:35:59 UTC
List:
ruby-core #77522
Issue #12820 has been updated by Matthew Kerwin.
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?
----------------------------------------
Feature #12820: Shorter syntax for assigning a method argument to an instance variable
https://bugs.ruby-lang.org/issues/12820#change-60786
* Author: Fabio Pugliese Ornellas
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
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.
--
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>