Forum: Ruby singleton methods vs. class instance vars

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8ae090eb7e9142e627fd25cbd3e1a7d5?d=identicon&s=25 Peter Lacey (Guest)
on 2006-01-04 00:48
(Received via mailing list)
Hi,

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
This topic is locked and can not be replied to.