Skip to content

Instantly share code, notes, and snippets.

@glittershark
Last active November 30, 2015 22:34
Show Gist options
  • Save glittershark/7fae1ea0295a30c300c5 to your computer and use it in GitHub Desktop.
Save glittershark/7fae1ea0295a30c300c5 to your computer and use it in GitHub Desktop.

This doesn't work

  module Mixin
    def def_a_method
      define_method(:foo) { puts 'mixin' }
    end
  end

  class Thing
    extend Mixin

    def_a_method

    def foo
      super
    end
  end

  puts Thing.new.foo
  # => -:13:in `foo': super: no superclass method `foo' for #<Thing:0x000000018bcf58> (NoMethodError)
  #    => from -:17:in `<main>'

But this does

  module Mixin
    def def_a_method
      include(Module.new do
        define_method(:foo) { puts 'mixin' }
      end)
    end
  end

  class Thing
    extend Mixin

    def_a_method

    def foo
      super
    end
  end

  puts Thing.new.foo
  # => mixin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment