Ruby: Automatically set instance variable as method argument?

前端 未结 3 1663
时光说笑
时光说笑 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:38

    Well, actually...

    class User
      define_method(:initialize) { |@name| }
    end
    
    User.new(:name).instance_variable_get :@name
    # => :name
    

    Works in 1.8.7, but not in 1.9.3. Now, just where did I learn about this...

提交回复
热议问题