Class Extension

Normally when including modules, class/module methods are not extended. To achieve this behavior requires some clever Ruby Karate. Instead class_extension provides an easy to use and clean solution. Simply place the extending class methods in a block of the special module method class_extension.

  module Mix
    def inst_meth
      puts 'inst_meth'
    end

    class_extension do
      def class_meth
        "Class Method!"
      end
    end
  end

  class X
    include Mix
  end

  X.class_meth  #=> "Class Method!"

History

Thanks to Trans, Nobu and Ulysses for their original on this concept.

Authors

  • Daniel Schierbeck
  • Thomas Sawyer
  • Nobu Nakada
  • Ulysses

Copying

Copyright (c) 2006 Daniel Schierbeck

Ruby License

This module is free software. You may use, modify, and/or redistribute this software under the same terms as Ruby.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.