Project

General

Profile

Feature #12820

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

Hello, 

 The pattern: 

 ~~~ruby ~~~ 
 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 

 ~~~crystal ~~~ 
 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: 

 ~~~ruby ~~~ 
 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.

Back