通常、Ruby オブジェクトの特定のインスタンスのメタクラスを取得するには、次のようにします。
class C
def metaclass
class << self; self; end
end
end
# This is this instance's metaclass.
C.new.metaclass => #<Class:#<C:0x01234567>>
# Successive invocations will have different metaclasses,
# since they're different instances.
C.new.metaclass => #<Class:#<C:0x01233...>>
C.new.metaclass => #<Class:#<C:0x01232...>>
C.new.metaclass => #<Class:#<C:0x01231...>>
任意のクラスの任意のオブジェクト インスタンスのメタクラスを知りたいだけで、のクラスで (または同様の) メソッドobj
を定義したくないとします。metaclass
obj
それを行う方法はありますか?