From: Jim Freeze Date: 2002-08-25T10:23:20+09:00 Subject: Re: same method as class and instance methods? On Sun, Aug 25, 2002 at 03:40:36AM +0900, Ferenc Engard wrote: > Hi there, > > I want to use a method as a class method, but as I use it frequently in > the classes' other methods, I do not want to prefix the method name with > class name in these cases. Is there a language construct to use a method > both as class and as instance methods? > > Anyway, I have a workaround as defining a method like: > > def methodName > ClassName.methodName > end > > Thanks: > Circum That is very clever. class MyClass def meth self.type.meth end def self.meth puts "MyClass::meth" end end MyClass.meth #=> MyClass::meth MyClass.new.meth #=> MyClass::meth What about creating an attr_instance_method module like: class Module def attr_instance_method(*class_methods) class_methods.each { |class_method| class_eval %{ def #{class_method} self.type.#{class_method} end } } end end#attr_instance_method So, the above would be class MyClass attr_instance_method :meth def self.meth puts "MyClass::meth" end end -- Jim Freeze If only I had something clever to say for my comment... ~