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

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)

On 5/5/07, John J. [email protected] 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

John J. 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.

On May 6, 2007, at 6:06 AM, Robert D. 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 J.

On May 6, 2007, at 6:15 AM, Tim H. 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)

On 5/5/07, John J. [email protected] 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/09/13/on-variables-values-and-objects

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/

On May 5, 2007, at 6:02 PM, Rick DeNatale wrote:

On 5/5/07, John J. [email protected] 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 W.

On May 6, 2007, at 9:41 AM, Gary W. wrote:

[…]
}

output

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

Gary W.
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.

On May 6, 2007, at 1:04 PM, John J. wrote:

an object is tainted or not. ("I’m making it plain English
@foo = ‘bar’

Gary W.
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.

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!~

On 05.05.2007 23:19, John J. 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. :slight_smile:

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

On 5/6/07, Robert K. [email protected] 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” :frowning:
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?

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

On 5/6/07, John J. [email protected] wrote:

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

In message [email protected], John J.
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

On May 6, 2007, at 7:39 PM, Robert D. 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 M.”

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

p1 Mickey M., 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.