Same method as class and instance methods?

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

···

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


Jim Freeze
If only I had something clever to say for my comment…
~