MVP
MVP

Reputation: 1111

Ruby: is_a? and instance_of? in BasicObject

How do is_a? and instance_of? methods work with a subclass of BasicObject ?

class My < BasicObject
  DELEGATE = [:is_a?, :instance_of?]
  def method_missing(name, *args, &blk)
    superclass unless DELEGATE.include? name
    ::Kernel.send(name,*args, &blk) 
  end
end

my = My.new
my.is_a? BasicObject  #=> true
my.is_a? My           #=> false ???
my.instance_of? My    #=> false ???

Upvotes: 0

Views: 406

Answers (2)

Stefan
Stefan

Reputation: 114208

You can steal is_a? from Kernel:

class My < BasicObject
  define_method(:is_a?, ::Kernel.method(:is_a?))
end

m = My.new
m.is_a?(My)          #=> true
m.is_a?(BasicObject) #=> true
m.is_a?(Object)      #=> false

If you're going to build your own object hierarchy, you could also define your own Kernel, something like:

module MyKernel
  [:is_a?, :instance_of?, :class].each do |m|
    define_method(m, ::Kernel.method(m))
  end
end

class My < BasicObject
  include ::MyKernel
end

Upvotes: 3

Schwern
Schwern

Reputation: 165200

::Kernel.send(name,*args, &blk) calls the method name on the class Kernel with the arguments args and the block &blk.

When you run my.is_a? My name is :is_a?, *args is My, and &blk is nil. You're really running Kernel.is_a? My.

Instead, if you want to reimplement is_a? for BasicObject you can walk your class's ancestors...

  def is_a?(target)
    # I don't know how to get the current class from an instance
    # that isn't an Object, so I'm hard coding the class instead.
    return ::My.ancestors.include?(target)
  end

Upvotes: 3

Related Questions