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)

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

···

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)

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

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.

···

--
RMagick [http://rmagick.rubyforge.org]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]

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

···

On May 6, 2007, at 6:06 AM, Robert Dober wrote:

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

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 May 6, 2007, at 6:15 AM, Tim Hunter wrote:

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.

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.

···

On 5/5/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

On May 6, 2007, at 6:15 AM, Tim Hunter wrote:

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

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)

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

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

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?

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 05.05.2007 23:19, John Joyce wrote:

On May 6, 2007, at 6:06 AM, Robert Dober wrote:

On 5/5/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

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

···

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.

>
>>> 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
> 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?

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?

<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

···

On 5/6/07, Robert Klemme <shortcutter@googlemail.com> wrote:

On 05.05.2007 23:19, John Joyce wrote:
> On May 6, 2007, at 6:06 AM, Robert Dober wrote:
>> On 5/5/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

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

···

--
Posted via http://www.ruby-forum.com/.

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 9:41 AM, Gary Wright wrote:

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

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.

···

On May 6, 2007, at 7:39 PM, Robert Dober wrote:

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

In fact, I might alias the eval methods as "evil" until I get a grip on them.

···

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

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

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

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.

<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:

···

On 5/6/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
#
@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

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

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

···

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.