Sam
Sam

Reputation: 321

What is the proper way to override is_a? and kind_of?

ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10.5.0]

Am I missing something?

module Mixin
  def is_a?(o)
    return false if o == Hash
    super
  end
  alias_method :kind_of?, :is_a?
end

class Doc < Hash
  include Mixin
end

puts Doc.new().is_a?(Doc) # => true
puts Doc.new().kind_of?(Doc) # => super: no superclass method `is_a?'

Expected:

puts Doc.new().is_a?(Doc) # => true
puts Doc.new().kind_of?(Doc) # => true
puts Doc.new().is_a?(Hash) # => false
puts Doc.new().kind_of?(Hash) # => false

Everything is cool on rubinius and ruby 1.9.2p136

Upvotes: 3

Views: 1054

Answers (1)

Sam
Sam

Reputation: 321

Thanks to everyone. This is a bug in 1.8.7 http://redmine.ruby-lang.org/issues/show/734

Upvotes: 2

Related Questions