Is it possible from a class to grab the name of the object instance?

Hello Marcin,

self.class.to_s should work

Sorry this return me the class name itself not the instance name?

e.g.
#!/usr/bin/env ruby

class Investment
  def initialize
    puts "I'am ALIVE!"
    iname=self.class.to_s
    puts "Instance Name: #{iname}"
  end
end

_Invest = Investment.new

puts _Invest.class

give me:
I'am ALIVE!
Instance Name: Investment
Investment

and not "Instance Name: _Invest" as am looking for.

Tx,
r.

There is no such thing as an instance name - unless you define a
property of your class called "name". Each instance can be referenced
by any number of variables including 0 - there is simply not _the_
name. As Inaki pointed out.

Kind regards

robert

···

2008/5/20 rubisher <rubisher@scarlet.be>:

Hello Marcin,

self.class.to_s should work

Sorry this return me the class name itself not the instance name?

e.g.
#!/usr/bin/env ruby

class Investment
def initialize
   puts "I'am ALIVE!"
   iname=self.class.to_s
   puts "Instance Name: #{iname}"
end
end

_Invest = Investment.new

puts _Invest.class

give me:
I'am ALIVE!
Instance Name: Investment
Investment

and not "Instance Name: _Invest" as am looking for.

--
use.inject do |as, often| as.you_can - without end

Robert Klemme wrote:

Hello Marcin,

self.class.to_s should work

Sorry this return me the class name itself not the instance name?

e.g.
#!/usr/bin/env ruby

class Investment
def initialize
   puts "I'am ALIVE!"
   iname=self.class.to_s
   puts "Instance Name: #{iname}"
end
end

_Invest = Investment.new

puts _Invest.class

give me:
I'am ALIVE!
Instance Name: Investment
Investment

and not "Instance Name: _Invest" as am looking for.

There is no such thing as an instance name - unless you define a
property of your class called "name".

Ok what I did.
The idea was that as far as ruby is interpreded, it have to save variable name somewhere in clear with its id and for debuging reason it could be accessible somehow. But ok it's not possible and for me it's just nice to know.

Each instance can be referenced
by any number of variables including 0 - there is simply not _the_
name. As Inaki pointed out.

Kind regards

robert

Tx to all for feedback,
  r.

···

2008/5/20 rubisher <rubisher@scarlet.be>:

rubisher wrote:

The idea was that as far as ruby is interpreded, it have to save
variable name somewhere in clear with its id and for debuging reason it
could be accessible somehow. But ok it's not possible and for me it's
just nice to know.

Well, you could modify your MyClass#initialize method to do something
similar.

Add a global variable or something similar, and add the object to this
variable (for example a Hash, with the variable name as key, and the
object's class as value).

Though, you'd have to intercept the assignment somehow to grab the
variable name.

HTH,
- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

Format a program to help the reader understand it.
~ - The Elements of Programming Style (Kernighan & Plaugher)

Phillip Gawlowski wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

rubisher wrote:

> The idea was that as far as ruby is interpreded, it have to save
> variable name somewhere in clear with its id and for debuging reason it
> could be accessible somehow. But ok it's not possible and for me it's
> just nice to know.

Well, you could modify your MyClass#initialize method to do something
similar.

Add a global variable or something similar, and add the object to this
variable (for example a Hash, with the variable name as key, and the
object's class as value).

Good, very good idea, I will investigate.

Though, you'd have to intercept the assignment somehow to grab the
variable name.

Thanks a lot,
  r.

···

HTH,
- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

Format a program to help the reader understand it.
~ - The Elements of Programming Style (Kernighan & Plaugher)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkg3bS8ACgkQbtAgaoJTgL/bPwCfRra/JfzsjjD5XS9cRXdB7Ede
RRUAn3eYv1QZQnWHIIzVixr1hm1eDwUR
=vkQ1
-----END PGP SIGNATURE-----