Singleton method error from bind when called with the same metaclass

425 Views Asked by At

I'm writing aspect-ish code more or less lifted from the chosen solution for this question that looks like the following:

class Module
  def add_logging klass, *method_names
    method_names.each do |method_name|
      original_method = instance_method method_name
      define_method method_name do |*args, &blk|
        log.debug("#{klass}.#{method_name} called")
        original_method.bind(klass).call(*args, &blk)
      end
    end
  end
end

The solution in the other post doesn't require the klass parameter, but it only works for instance methods, whereas I hope to call my code like this:

module MyModule
  def MyModule.module_method
    p "hello"
  end
  class << self
    add_logging self, :module_method1
  end
end

Unfortunately when I run this code I get in 'bind': singleton method called for a different object (TypeError). Seeing as I pass self in the context of the class << self block, I don't understand why the bind call in the above code thinks it isn't binding to the exact same metaclass.

1

There are 1 best solutions below

4
On BEST ANSWER

This should work for you:

class Module
  def add_logging(*method_names)
    method_names.each do |method_name|
      original_method = method(method_name).unbind
      define_singleton_method(method_name) do |*args, &blk|
        puts "#{self}.#{method_name} called"
        original_method.bind(self).call(*args, &blk)
      end
    end
  end
end

# class method example
module MyModule
  def self.module_method1
    puts "hello"
  end

  add_logging :module_method1
end

MyModule.module_method1

# output:
#
# MyModule.module_method1 called
# hello