Skip to content

Avoid the anti-pattern of `def self.included(base); base.extend …; end`

License

Notifications You must be signed in to change notification settings

apeiros/inherit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README

Summary

Avoid the anti-pattern of def self.included(base); base.extend …; end, without getting lost in subclassing.

Installation

gem install inherit

Usage

require 'inherit'

module Inheritable
  module Constants
    Foo = "Nice constant!"
  end
  module ClassMethods
    def funky
      "Funky class method!"
    end
  end
  module InstanceMethods
    def rad
      "Rad instance method!"
    end
  end
  def self.inherited(subclass)
    puts "#{self} got inherited by #{subclass}"
  end
  def self.extended(object)
    puts "#{self} extended #{object}"
  end
end

class Example
  inherit Inheritable # -> "Inheritable got inherited by Example"
end
Example::Foo                  # => "Nice constant!"
Example.funky                 # => "Funky class method!"
Example.new.rad               # => "Rad instance method!"
a_string = "a string"
a_string.inherit Inheritable  # -> "Inheritable extended a string"
a_string.singleton_class::Foo # => "Nice constant!"
a_string.rad                  # => "Rad instance method!"

Description

Avoid the anti-pattern of def self.included(base); base.extend …; end, without getting lost in subclassing.

Links

License

You can use this code under the {file:LICENSE.txt BSD-2-Clause License}, free of charge. If you need a different license, please ask the author.

About

Avoid the anti-pattern of `def self.included(base); base.extend …; end`

Resources

License

Stars

Watchers

Forks

Packages

No packages published