开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜