Get Variable Name from object_id

Discussion in 'Ruby' started by Evan Moseman, Jan 12, 2008.

  1. Evan Moseman

    Evan Moseman Guest

    If I am perusing through the each_object loop and find the objects I'm
    interested in, how can I get their variable names?

    Thanks!

    --
    Evan
    Evan Moseman, Jan 12, 2008
    #1
    1. Advertising

  2. Evan Moseman

    Tim Hunter Guest

    Evan Moseman wrote:
    > If I am perusing through the each_object loop and find the objects I'm
    > interested in, how can I get their variable names?
    >
    > Thanks!
    >
    > --
    > Evan
    >


    You can't. Variables are just labels for objects. What if you have
    something like this:

    foo = MyClass.new
    bar = foo

    What's the variable name of the object? What about

    ary = []
    ary << bar

    Now what's the variable name? What about this

    def f(v)
    puts v
    end

    f(ary[0])

    Now what's the variable name?

    --
    RMagick: http://rmagick.rubyforge.org/
    RMagick 2: http://rmagick.rubyforge.org/rmagick2.html
    Tim Hunter, Jan 12, 2008
    #2
    1. Advertising

  3. Evan Moseman

    Todd Benson Guest

    On Jan 12, 2008 5:13 PM, Tim Hunter <> wrote:
    >
    > Evan Moseman wrote:
    > > If I am perusing through the each_object loop and find the objects I'm
    > > interested in, how can I get their variable names?
    > >
    > > Thanks!
    > >
    > > --
    > > Evan
    > >

    >
    > You can't. Variables are just labels for objects. What if you have
    > something like this:
    >
    > foo = MyClass.new
    > bar = foo
    >
    > What's the variable name of the object? What about
    >
    > ary = []
    > ary << bar
    >
    > Now what's the variable name? What about this
    >
    > def f(v)
    > puts v
    > end
    >
    > f(ary[0])
    >
    > Now what's the variable name?


    I was going to reply in the same way, but I suppose it would be
    interesting to call up names for objects if the names exist for the
    object space (as nil or array for each object maybe?). Like
    Object#references or something like that.

    Todd
    Todd Benson, Jan 13, 2008
    #3
  4. Evan Moseman

    botp Guest

    On Jan 13, 2008 6:44 AM, Evan Moseman <> wrote:
    > interested in, how can I get their variable names?


    thinking about the subject, i think you want to refer to the object fr
    a given object id, mayb try

    ObjectSpace#_id2ref

    kind regards -botp
    botp, Jan 13, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bobby Chamness
    Replies:
    2
    Views:
    2,380
    Joe Smith
    Apr 22, 2007
  2. Hank Gong
    Replies:
    6
    Views:
    100
    Eero Saynatkari
    Dec 21, 2005
  3. uncutstone

    which one is object_id?

    uncutstone, May 9, 2006, in forum: Ruby
    Replies:
    1
    Views:
    125
    Robert Klemme
    May 9, 2006
  4. Peña, Botp

    get info from object_id

    Peña, Botp, May 31, 2007, in forum: Ruby
    Replies:
    4
    Views:
    122
    Peña, Botp
    Jun 1, 2007
  5. Meinrad Recheis
    Replies:
    5
    Views:
    117
Loading...

Share This Page