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

**URL:** <https://www.ruby-forum.com/t/is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/137948>\
**Category:** Ruby\
**Created:** [May 18, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/137948 "2008-05-18T19:18:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 18, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/137948/1 "2008-05-18T19:18:44Z")

</div>

Hello all rubyist,

I fall in love of ruby some years ago when I reach to re-write in a  
couple of days with ruby only a scripts I spent some  
weeks to write with classical unix tools (awk, sed, grep, …) and the  
new stuff was running far more quickly.

To learn more ruby, I tried also to write a script to build the linux  
toolchain (i.e. binutils, glibc, gcc).  
As far as for those tools the build sequence is the same (cleanup a  
previous build, configure, make, install), it was easy  
to design a class which I named TCModule to run each of those steps for  
each module (binutils, glibc, gcc).

When something went wrong some where, I like to log outputs produced by  
each method in a file.  
Idealy, this file should have a name with the module name (e.g.  
binutils) and the on going step, typically the method name.

Till now, to create this log file name, I didn’t find better way to  
create variabales to own to those values like:

[snip]

# Class of ToolChain Module: binutils|gcc|glibc

class TCModule  
# make accessible object variables  
attr\_accessor :ModuleName, :Target, :Host, :Config, :Prefix,  
:XcPath,   
:Install, :Env, :Extra  
attr :CurrentMethod

```
 # methode called by new to initialise object variables
 def initialize(_ModuleName, _Target, _Host, _SpecConfig, _Prefix, 

```

\_XcPath,   
\_Install, \_Env, \_Extra)  
@ModuleName = \_ModuleName # name of module to  
build  
[snip]  
@CurrentMethod = nil # On going action name  
@LogPrefx = “#{$LogDir}/#{@ModuleName}#{@Extra}-#{LaunchDate}”

```
 end # initialize

```

[snip]

```
 # configure
 def _Configure
     @CurrentMethod = "_Configure"

```

[snip]  
end # \_Configure

```
 # install
 def _Install
     @CurrentMethod = "_Install"

     return make("#{@CurrentMethod}", "install")
 end # _Install

```

end # class TCModule

[snip]

which is initialized like:  
\_Binutils = TCModule.new(:\_Binutils.id2name, # ModuleName: name of  
module to build  
Target, # Target: target we’re  
building for  
LocalMach, # Host: compiler to  
use  
$BinUtilsConfigP1, # Prefix: where to  
install  
$TempXC, # SpecConfig: Extra  
config parameters  
$TempXC, # XcPath: xcompiler  
Path  
“”, # Install:  
“”, # Env: Extra env  
parameters  
“”) # Extra: Xtra release  
info (pass1, 2)

But to make maintenance easiest, isn’t it possible in the methods to  
grab the name of the object (instance) (my ModuleName)  
for which it’s on going to run?

Tx in advance for ideas,  
r.

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 19, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/137948/2 "2008-05-19T17:42:34Z")

</div>

2008/5/18, rubisher [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> But to make maintenance easiest, isn’t it possible in the methods to grab  
> the name of the object (instance) (my ModuleName) for which it’s on going to  
> run?

I remember asking if it’s possible to get a variable name using any  
variable method and the answer was “no”. So I assume the same for  
objects since basically are the same.

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 19, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/137948/3 "2008-05-19T18:56:57Z")

</div>

self.class.to\_s should work

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 24, 2008, 3:11am UTC](https://www.ruby-forum.com/t/is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/137948/4 "2008-05-24T03:11:24Z")

</div>

> a class which I named TCModule to run each of those steps for  
> each module (binutils, glibc, gcc).

Is your code available somewhere? Especially the above quoted part  
spiked my interest, as I was not aware one could decouple an  
autoconfigured, running build-way (with or in ruby).

---

<div class="post-metadata">

**Author:** ![rubisher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rubisher](https://www.ruby-forum.com/u/rubisher)\
**Post date:** [May 23, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/is-it-possible-from-a-class-to-grab-the-name-of-the-object-instance/137948/5 "2008-05-23T23:04:10Z")

</div>

IÃ±aki Baz C. wrote:

> 2008/5/18, rubisher [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > But to make maintenance easiest, isn’t it possible in the methods to grab  
> > the name of the object (instance) (my ModuleName) for which it’s on going to  
> > run?
> 
> I remember asking if it’s possible to get a variable name using any  
> variable method and the answer was “no”.  
> Yes I remember vaguely to have read something like this some where  
> (obvioulsy no means to find back when you want).  
> I also had the filling it was already sometime ago, so in the mean time  
> it could be possible this changed in the  
> implementation of ruby (I was to busy this last week to order the 3rd  
> Edition of “Programming Ruby” but I will do asap 😉

> So I assume the same for  
> objects since basically are the same.

Tx for feedback,  
r.
