Singleton methods vs. class instance vars


#1

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