get instance variable name from itself in ruby

后端 未结 4 1031
花落未央
花落未央 2021-01-13 11:43

I have an instance variable @foo and I want to write some code so that I get string \'foo\'

any hint?

4条回答
  •  广开言路
    2021-01-13 12:14

    In Ruby, you can only manipulate objects. Variables (including instance variables) aren't objects.

    Besides, what do you want your magic method to return in this case:

    foo = Object.new
    bar = foo
    @baz = bar
    @qux = bar
    
    magic_method(foo) # what should the return value be and how would it know?
    

提交回复
热议问题