Love U Ruby
2013-Sep-06 07:37 UTC
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Emil S
2013-Sep-06 09:17 UTC
Re: How to get local variable name from object id or memory reference in ruby?
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 object has no way of finding out
which variable references itself.
But one inefficient way to achieve what you want would be to iterate
through all the local variables and print the names of all local variables
that has a particular object ID.
a = b = "hello world"
local_variables.each {|x| puts x if (eval x.to_s).object_id == a.object_id}
On 6 September 2013 13:07, Love U Ruby
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:
> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> 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.
>
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/CAJ%3Dox-Acv-2cASV22H6_ypnNtFQtxwAPMEgS0RCO-Mp1Z%3D97wg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
Apparently Analagous Threads
- Different versions, different results ?
- Rails 4 + AngularJs (unembedded client) integration sample
- Challenge for object_id, Garbage Value
- A Proposal To Magically Remove ''params''
- You have already activated rake 10.0.4, but your Gemfile requires rake 10.0.3.