Getting the class name from within the initialize method when class inherited from Hash
I have开发者_StackOverflow a class that inherits from Hash
. When this class itself gets inherited from, I want to know the class name of the inheriting class from within the initialize
method. When I call self
I get {}
, which doesn't know of the name
method.
class Foo < Hash
def initialize
# Here i want to know that the class is Foo
end
end
How do I get the class name?
It’s very simple: self.class.name
Daniel Brockman's answer will return you the string if you want to do a check:
if self.kind_of?(Foo)
#whatever you want
end
The thing is due to the intent of the initializer, when you call Foo.new the instance will always be an instance of the class Foo or child, so I'm confused about what you're trying to do.
精彩评论