[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

John Joyce

5/5/2007 8:56:00 PM

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)

13 Answers

Robert Dober

5/5/2007 9:07:00 PM

0

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
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Tim Hunter

5/5/2007 9:15:00 PM

0

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.rub...]
RMagick Installation FAQ [http://rmagick.rub.../install-faq.html]


John Joyce

5/5/2007 9:20:00 PM

0


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

John Joyce

5/5/2007 9:23:00 PM

0


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

5/5/2007 10:03:00 PM

0

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)

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.denh...articles/2006/09/13/on-variables-values-a...

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

Gary Wright

5/6/2007 12:42:00 AM

0


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




gz zz

5/6/2007 1:18:00 AM

0

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

John Joyce

5/6/2007 4:04:00 AM

0


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.

John Joyce

5/6/2007 4:47:00 AM

0


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.
>
In fact, I might alias the eval methods as "evil" until I get a grip
on them.

Robert Klemme

5/6/2007 10:09:00 AM

0

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

> 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