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

2013-09-06 Thread Love U 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

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

2013-09-06 Thread Emil S
I don't think that will be possible. A variable has an identifier ( in your case, n1 ) and a reference to an object ( Sample:0x13bc648 ) so when you say n1 , it knows which object it references and returns that. As far as I know Ruby doesn't keep track of reference counts of an object and the