Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

How to define a singleton class method

Mitch VanDuyn edited this page Jan 21, 2017 · 1 revision
class Foo 
  def self.inherited(child)
    child.singleton_class.define_singleton_method(:receives) do |name, &block|
      define_method(name) do |*args|
        puts "yes I added this code here first!!!!"
        block.call *args
      end
    end
  end
end

class Bar < Foo
  class << self 
    receives(:surprise!) {'surprise!'}
  end
end

puts Bar.surprise!
Clone this wiki locally