Ruby: Automatically set instance variable as method argument?

前端 未结 3 1664
时光说笑
时光说笑 2021-01-12 02:10

Are there any plans to implement ruby behavior similar to the CoffeeScript feature of specifying an instance variable name in a method argument list? Like

cl         


        
3条回答
  •  孤城傲影
    2021-01-12 02:20

    I think you answered your own question, it does not fit the ruby simplicity philosophy. It would add additional complexity for how parameters are handled in methods and moves the logic for managing variables up into the method parameters. I can see the argument that it makes the code less readable a toss up, but it does strike me as not very verbose.

    Some scenarios the @ param would have to contend with:

    def initialize( first, last, @scope, @opts = {} )
    
    def search( @query, condition )
    
    def ratchet( @*arg  ) 
    

    Should all of these scenarios be valid? Just the initialize? The @*arg seems particularly dicey in my mind. All these rules and exclusions make the Ruby language more complicated. For the benefit of auto instance variables, I do not think it would be worth it.

提交回复
热议问题