Forum: Ruby How can I output an object/variable's name?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-05-05 22:57
(Received via mailing list)
Suppose I have an object :

a = 3

I want to output the name a and the contents 3
Outputing the contents is no problem.
Is  there a method to output the name of the variable (object)
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-05 23:08
(Received via mailing list)
On 5/5/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
> Suppose I have an object :
>
> a = 3
>
> I want to output the name a and the contents 3
> Outputing the contents is no problem.
> Is  there a method to output the name of the variable (object)

No there is no way to output the name of a variable as a variable is
one of the view concepts in Ruby that are not directly accessible from
inside Ruby.
When it comes to objects things change of course, some objects have
name properties as e.g. classes

irb(main):002:0> A=Class.new
=> A
irb(main):003:0> A.name
=> "A"
But be aware that it is the object and not the constant referring to
it who has the name, as one can easily see...
irb(main):007:0* b = Class.new
=> #<Class:0xb7d9ce54>
irb(main):008:0> b.name
=> ""
irb(main):009:0> C = b
=> C
irb(main):010:0> C.name
=> "C"
irb(main):011:0> b.name
=> "C"

this is maybe a little bit confusing but that is what ruby does when a
class object is assigned to a constant.

Robert
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2007-05-05 23:15
(Received via mailing list)
John Joyce wrote:
> Suppose I have an object :
>
> a = 3
>
> I want to output the name a and the contents 3
> Outputing the contents is no problem.
> Is  there a method to output the name of the variable (object)
>
There's no way to do this in general.
Why don't you describe what you're trying to do? Maybe somebody can
suggest an alternative.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-05-05 23:21
(Received via mailing list)
On May 6, 2007, at 6:06 AM, Robert Dober wrote:

> one of the view concepts in Ruby that are not directly accessible from
> irb(main):007:0* b = Class.new
> this is maybe a little bit confusing but that is what ruby does when a
> class object is assigned to a constant.
>
> Robert
Thanks Robert, but...

That is totally confusing.
So what you are saying is that some objects have names and some do not.
Since everything (almost) is an object, which objects or which
classes would have names?
Oh, maybe I should go to bed. it's 6:20am here in Japan.

What about injecting my own name properties into a class, could I do
this type of injection in a code block?

John Joyce
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-05-05 23:23
(Received via mailing list)
On May 6, 2007, at 6:15 AM, Tim Hunter wrote:

> Why don't you describe what you're trying to do? Maybe somebody can
> suggest an alternative.

What I'd like to do is similar to the idea of processing files and
outputing results with names of files.
I want to do the same with objects.
I wrote an little code block just to talk back to me and tell me if
an object is tainted or not. ("I'm making it plain English sentences,
rather than simply true or false)
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-05-06 00:03
(Received via mailing list)
On 5/5/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
> >> Is  there a method to output the name of the variable (object)
> rather than simply true or false)
John,

This is a common stumbling block for a lot of folks who haven't seen
a uniformly object oriented language.

I've written about this a bit in my blog some time ago,
http://talklikeaduck.denhaven2.com/articles/2006/0...

Variables are just names for objects, and aren't properties of the
objects themselves.  Objects don't know what folks are calling them.
Imagine that you have a secret admirer, she calls you 'that cute guy
who lives down the street.' but you don't know that.  Some guy who
likes her calls you 'that jerk who she thinks is cute.'  You don't
know about either of those 'names' but they both refer to you in
particular contexts.

I've been meaning to write a series of articles about variables in ruby.

It's just a question of finding the time.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2007-05-06 02:42
(Received via mailing list)
On May 5, 2007, at 6:02 PM, Rick DeNatale wrote:
> On 5/5/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
>> What I'd like to do is similar to the idea of processing files and
>> outputing results with names of files.
>> I want to do the same with objects.
>> I wrote an little code block just to talk back to me and tell me if
>> an object is tainted or not. ("I'm making it plain English sentences,
>> rather than simply true or false)
> [..]
> Variables are just names for objects, and aren't properties of the
> objects themselves.  Objects don't know what folks are calling them.

You can't go from an arbitrary object reference to an identifier, but
you can go from an identifier to an object reference via eval:

a = 42
@foo = 'bar'
["a", "@foo", "Array"].each { |id|
   puts "#{id} references a #{eval(id).class}"
}

# output

a references a Fixnum
@foo references a String
Array references a Class



Gary Wright
Ba608f20b6707017d9812b9b3f1406e3?d=identicon&s=25 gpy good (gpygood)
on 2007-05-06 03:17
output an object/variable's name:

class Person
  def love(name)
    # out put a love b
  end
end

boy = Person.new
girl=Person.new
boy.love(girl) #=>should output "boy love girl"

I think this is very nice feature!~
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-05-06 06:04
(Received via mailing list)
On May 6, 2007, at 9:41 AM, Gary Wright wrote:

>> [..]
> }
>
> # output
>
> a references a Fixnum
> @foo references a String
> Array references a Class
>
>
>
> Gary Wright
Gary I think this does what I'm looking for. It's a little hackish to
me, but it seems to work.
I find eval to be one of those less obvious tools. I was just reading
up on eval and its cadre of similar methods last night. A very
mystical bunch to me still.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-05-06 06:48
(Received via mailing list)
On May 6, 2007, at 1:04 PM, John Joyce wrote:

>>>> an object is tainted or not. ("I'm making it plain English
>> @foo = 'bar'
>>
>>
>> Gary Wright
> Gary I think this does what I'm looking for. It's a little hackish
> to me, but it seems to work.
> I find eval to be one of those less obvious tools. I was just
> reading up on eval and its cadre of similar methods last night. A
> very mystical bunch to me still.
>
In fact, I might alias the eval methods as "evil" until I get a grip
on them.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-05-06 12:10
(Received via mailing list)
On 05.05.2007 23:19, John Joyce wrote:
>>> Is  there a method to output the name of the variable (object)
>> => "A"
>> irb(main):011:0> b.name
> Since everything (almost) is an object, which objects or which classes
> would have names?

No, he is saying that there are classes whose instances have a property
called "name".  But this has absolutely nothing to do with variable
names.  If you think about it for a moment, it does not really make
sense to want to get a variable name from an object.  Consider

a = b = Object.new

Now, there is just one instance - which "name" would you like to get?

> Oh, maybe I should go to bed. it's 6:20am here in Japan.

Sleep always helps. :-)

> What about injecting my own name properties into a class, could I do
> this type of injection in a code block?

I am not sure what you mean by "injection".  But you can set instance
variables - if you wish so.  But you might run into trouble if you
overwrite a property that's crucial for the instance.  Look at
instance_variables and *set and *get.

Kind regards

  robert
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-06 12:40
(Received via mailing list)
On 5/6/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >>> Outputing the contents is no problem.
> >> irb(main):003:0> A.name
> >> => "C"
> > So what you are saying is that some objects have names and some do not.
> > Since everything (almost) is an object, which objects or which classes
> > would have names?
>
> No, he is saying that there are classes whose instances have a property
> called "name".  But this has absolutely nothing to do with variable
> names.
You are right Robert.
As a matter of fact I have produced yet another masterpiece of "Not
understanding OP's needs" :(
But I really loved Rick's way to explain things.
> If you think about it for a moment, it does not really make
> sense to want to get a variable name from an object.  Consider
>
> a = b = Object.new
>
> Now, there is just one instance - which "name" would you like to get?
>
<snip>

However John I still fail to see why you were happy with "eval" why
would you need it?
If you feel the need for eval in this context it is for 99% wrong,
maybe we can help you better if you describe a little bit more of your
needs.

Maybe all you need are objects with a name property and maybe it would
be best to use a different name (see how the two levels that
designation can be used might become *very* confusing) like e.g.
how_I_call_this_object (that is an extreme example of course).

Cheers
Robert
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-05-06 13:46
(Received via mailing list)
On May 6, 2007, at 7:39 PM, Robert Dober wrote:

> how_I_call_this_object (that is an extreme example of course).
>
> Cheers
> Robert

Well, exactly all I want to do is to be able to take a set of
objects, and then list them by the name or reference (the one used in
program code) then list them and their contents. I don't really need
to do it. I just was kind of wondering if it was really possible or
practical, but it seems to not be.

example would be:

class Person
    attr_accessor :name
    some other attributes ...
end

p1 = Person.new
p1.name = "Mickey Mouse"

So I'd like to generate a list like so:

p1  Mickey Mouse, attribute, attribute, ...
p2  attribute, attribute, ...
....

maybe it's not necessary to do this. I want the instance's name (like
p1 here) to be accessible and manipulatable like an attribute as well.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-06 13:56
(Received via mailing list)
On 5/6/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
<snip>
> maybe it's not necessary to do this. I want the instance's name (like
> p1 here) to be accessible and manipulatable like an attribute as well.
>
Maybe there is a slight missunderstanding between local variables and
instance_variables

Look at this

528/28 > cat inst-vars.rb && ruby inst-vars.rb
# vim: sts=2 sw=2 tw=0 expandtab nu:
#
@a = 42
@b = "fourtytwo"
@c = {:a=>42,:b=>22}
@d = Object.new

instance_variables.each do
  |ivar|
  puts "instance variable #{ivar} refers to object: " <<
     instance_variable_get(ivar).inspect
end
instance variable @d refers to object: #<Object:0x2b68198>
instance variable @b refers to object: "fourtytwo"
instance variable @a refers to object: 42
instance variable @c refers to object: {:b=>22, :a=>42}

Yet there is still no way to go from the object to the instance
variable.
And yes you can achieve about the same for local variables with
local_variables.each do
  |lvar|

  value = eval(lvar)
...
end

I see why you needed eval now!!

Cheers
Robert
B8cfd5ec0f88bf5b5f2eedda7d1a0746?d=identicon&s=25 unknown (Guest)
on 2007-05-06 13:57
(Received via mailing list)
In message <C2DEFA07-FACE-48EB-981E-CAC197536D04@gmail.com>, John Joyce
writes:
>Well, exactly all I want to do is to be able to take a set of
>objects, and then list them by the name or reference (the one used in
>program code) then list them and their contents. I don't really need
>to do it. I just was kind of wondering if it was really possible or
>practical, but it seems to not be.

I don't think it is, in general.  Not every object has any names at all,
and some objects have multiple names.  That's not even dealing with
scoping...

-s
This topic is locked and can not be replied to.