class Foodef an_instance_methodputs "I am an instance method"enddef self.a_class_methodputs "I am a class method"endend
foo = Foo.new
def foo.a_singleton_methodputs "I am a singletone method"end
访问Eigenclass(包括单例方法)的另一种方法是使用以下语法(class <<):
foo = Foo.new
class << foodef a_singleton_methodputs "I am a singleton method"endend
现在您可以为self定义一个单例方法,它在上下文中是类Foo本身:
class Fooclass << selfdef a_singleton_and_class_methodputs "I am a singleton method for self and a class method for Foo"endendend