# Repost: singleton methods vs. class instance vars

**URL:** <https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481>\
**Category:** Ruby\
**Created:** [January 4, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481 "2006-01-04T18:32:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 4, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/1 "2006-01-04T18:32:54Z")

</div>

Hi,

Please excuse me if this message was already delivered. I’m sending  
it again as I suspect something went wrong with my original post  
because it didn’t get cross-posted to comp.lang.ruby and there were  
no replies from a typically very helpful audience.

Pete

Original message:

I’m liberally helping myself to some of the ActiveRecord code. In  
this code there are a handful of class methods that dynamically  
create singleton methods to override the base methods. A simplified  
version follows:

class SingletonBase  
class \<\< self  
def table\_name  
reset\_table\_name  
end

```
 def reset_table_name
   puts "reset_table_name called"
   name = self.name
   sing = class << self; self; end
   sing.class_eval "def table_name; #{name.inspect}; end"
   name
 end

```

end  
end

(I hope that simplifying the code didn’t remove anything of importance.)

In this code the first time SingletonBase#table\_name is called it  
calls SingletonBase#reset\_table\_name. reset\_table\_name in turn  
creates a singleton class and adds a method that overrides table\_name  
to return what was just calculated.

Fine.

But my question is, is there any reason to prefer this method over  
using class instance variables, which seem a little clearer and  
simpler? Ala:

class InstanceBase  
class \<\< self  
def table\_name  
@table\_name || reset\_table\_name  
end

```
 def reset_table_name
   puts "reset_table_name called"
   @table_name = self.name
 end

```

end  
end

If both of these classes are exercised as follows, the results are  
comparable:

class Sub\_SingletonBase1 \< SingletonBase  
p table\_name  
p table\_name  
end

class Sub\_SingletonBase2 \< SingletonBase  
p table\_name  
p table\_name  
end

class Sub\_InstanceBase1 \< InstanceBase  
p table\_name  
p table\_name  
end

class Sub\_InstanceBase2 \< InstanceBase  
p table\_name  
p table\_name  
end

Output:

reset\_table\_name called  
“Sub\_SingletonBase1”  
“Sub\_SingletonBase1”

reset\_table\_name called  
“Sub\_SingletonBase2”  
“Sub\_SingletonBase2”

reset\_table\_name called  
“Sub\_InstanceBase1”  
“Sub\_InstanceBase1”

reset\_table\_name called  
“Sub\_InstanceBase2”  
“Sub\_InstanceBase2”

Note that there are class methods in ActiveRecord that _do_ use class  
instance variables, but not all. In particular for setting/getting  
table\_name, primary\_key, inheritance\_column, and sequence\_name,  
basically everything that can be set by the user. So, what benefit  
is derived from using virtual/meta/singleton/eigen-classes over class  
instance variables?

Regards,  
Pete

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 4, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/2 "2006-01-04T21:24:45Z")

</div>

hello,

apparently ruby as most other languages has more than one way to  
express the same thing. i myself see no advantage of the code in active  
record that you posted and your code. except for the active record code  
is faster than yours because it does not check any variables at all  
when evaluating the table\_name method.

konstantin

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 4, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/3 "2006-01-04T21:48:57Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
ako… [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hello,
> 
> apparently ruby as most other languages has more than one way to  
> express the same thing. i myself see no advantage of the code in active  
> record that you posted and your code. except for the active record code  
> is faster than yours because it does not check any variables at all  
> when evaluating the table\_name method.

I’m not so sure it’s faster; it uses a class\_eval.

Phil

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 4, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/4 "2006-01-04T22:34:17Z")

</div>

On Jan 4, 2006, at 3:47 PM, Phil T. wrote:

> I’m not so sure it’s faster; it uses a class\_eval.

There’s a comment in the actual active record code that notes that  
class\_eval is used over define\_method to get around a memory leak in  
fcgi.

But to Ako’s point, it seems to me that there has to be some  
important difference between the singleton approach and the class  
instance var approach for several reasons:

1. DHH and the other active record coders and reviewers know what  
they’re doing, and I suspect this seemingly convoluted code wouldn’t  
have survived for very long if it was simply “another way of doing it.”

2. I’m very new to Ruby, and I don’t know what I’m doing. 🙂

3. The active record code _does_ use class instance vars in other  
places, so this looks very intentional.

Any other thoughts?

Regards,  
Pete

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 4, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/5 "2006-01-04T23:20:20Z")

</div>

Ahem, sorry.

On Wed, 04 Jan 2006 22:09:09 -0000, I forgot to write:

> Seems to me the technique is about one-time creation of attribute  
> accessors that, instead of looking like:

```
def name
  @name
end

```

look like this:

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 4, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/6 "2006-01-04T23:20:17Z")

</div>

On Wed, 04 Jan 2006 21:32:25 -0000, Peter Lacey  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> approach for several reasons:
> 
> 1. DHH and the other active record coders and reviewers know what  
> they’re doing, and I suspect this seemingly convoluted code wouldn’t  
> have survived for very long if it was simply “another way of doing it.”
> 
> 2. I’m very new to Ruby, and I don’t know what I’m doing. 🙂
> 
> 3. The active record code _does_ use class instance vars in other  
> places, so this looks very intentional.

Seems to me the technique is about one-time creation of attribute  
accessors that, instead of looking like:

```
def name
  "name"
end

```

Which, when called multiple times, may be faster than equivalent  
instance  
variable lookups. My _guess_ is that this is probably a bit quicker with  
symbols and maybe strings (which is what this seems to be using - notice  
the ‘inspect’ too) than going for an instance var.

?

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 5, 2006, 12:54am UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/7 "2006-01-05T00:54:26Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Peter Lacey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 4, 2006, at 3:47 PM, Phil T. wrote:
> 
> > I’m not so sure it’s faster; it uses a class\_eval.

ooops, I see that the class\_eval is only called once so maybe it’s  
correct  
that that version is faster overall.

> they’re doing, and I suspect this seemingly convoluted code wouldn’t  
> have survived for very long if it was simply “another way of doing it.”

> 1. The active record code _does_ use class instance vars in other  
> places, so this looks very intentional.

I wouldn’t be so sure… I suspect that DHH’s (and the other Rails  
contributors) Ruby skills and understanding have increased over time  
and maybe if they were writing the code from scratch now they would use  
the  
class instance variable method as opposed to the class\_eval method. I  
know  
that I’ve got a lot of Ruby code out there that I wrote early on that I  
would  
write very differently now… Sometimes code survives because it works.

> 1. I’m very new to Ruby, and I don’t know what I’m doing. 🙂
> 
> 2. The active record code _does_ use class instance vars in other  
> places, so this looks very intentional.

Like I said above, it’s possible that the class instance var meme came  
later  
in the development history of Rails and that they started using it after  
it  
became known to them. Only DHH can say for sure 😉

Phil

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 5, 2006, 2:12am UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/8 "2006-01-05T02:12:39Z")

</div>

Final post in this thread. It seems that the consensus is that these  
two approaches are equal in terms of their results. Knowing that, it  
would appear that I _did_ oversimplify my original post. For the  
sake of the question I had distilled the active record code to this:

class SingletonBase  
class \<\< self  
def table\_name  
reset\_table\_name  
end

```
 def reset_table_name
   puts "reset_table_name called"
   name = self.name
   sing = class << self; self; end
   sing.class_eval "def table_name; #{name.inspect}; end"
   name
 end

```

end  
end

However, the actual code is more (but not exactly, in the interest of  
brevity) like this:

class Base  
class \<\< self  
def table\_name  
reset\_table\_name  
end

```
 def reset_table_name
   name = self.name
   set_table_name name
   name
 end

 def set_table_name( value=nil, &block )
   sing = class << self; self; end
   if value
     sing.class_eval "def #{name}; #{value.to_s.inspect}; end"
   else
      sing.send :define_method, name, &block
    end
 end

```

end  
end

It would appear, then, that the singleton class is created solely for  
handling the optional block parameter, and not, as I originally  
suspected, for some more global reason.

Sorry for the wild goose chase and thank you for your insight.

Pete

---

<div class="post-metadata">

**Author:** ![Peter\_Lacey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9bd4f/32.png) [@Peter\_Lacey](https://www.ruby-forum.com/u/Peter_Lacey)\
**Post date:** [January 4, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/repost-singleton-methods-vs-class-instance-vars/89481/9 "2006-01-04T23:47:27Z")

</div>

> > On Jan 4, 2006, at 5:18 PM, Ross B. wrote:  
> > Which, when called multiple times, may be faster than equivalent  
> > instance variable lookups.

Maybe, but that’s a lot of optimization for a little problem. These  
four active record methods (table\_name, primary\_key, etc.) can’t be  
called so often that it becomes necessary to avoid the cost of  
dereferencing a variable.

There’s an article by _why the lucky stiff_ (http://  
[whytheluckystiff.net/articles/seeingMetaclassesClearly.html](http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html)) that  
gets tantalizingly close to answering this question, but not close  
enough. At least not for me. \_why, are you lurking? Any input?

I dunno, maybe I’m looking for something that’s not there, but I’d  
love to keep the discussion going for a bit, if I may.

Pete
