Friday, September 6, 2013

[Rails] How to get local variable name from object id or memory reference in ruby?

Follow the code :-


class Sample
def bar
p method(__method__).receiver # only `self` would do the trick.
end
end

n1=Sample.new # => #<Sample:0x13bc648>
n2=Sample.new # => #<Sample:0x13bc630>

n1.bar # => #<Sample:0x13bc648>
n2.bar # => #<Sample:0x13bc630>

But this is my try. I am looking for any method is available in
Ruby,into which say if I pas n1.object_id or #<Sample:0x13bc648>,I will
get back in return the local variable name say here it is - n1 ?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/60c21f9f1eb83f8b805ba08de75e7353%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate