GithubHelp home page GithubHelp logo

apeiros / inherit Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 0.0 224 KB

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

License: BSD 2-Clause "Simplified" License

Ruby 19.99% CSS 52.48% JavaScript 27.53%

inherit's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.