Coder Social home page Coder Social logo

module_creation_helper's Introduction

module_creation_helper

module_creation_helper adds a helper method for creating new modules and classes at runtime.

Resources

API

Bugs

Development

Testing

Source

  • git://github.com/pluginaweek/module_creation_helper.git

Mailing List

Description

Creating modules and classes at runtime isn’t the easiest and most intuitive process. Although often used for anonymous classes, there are many times where you will want to associate a runtime class with an actual name.

Traditionally, you would create new classes like so:

c = Class.new               # => #<Class:0x480e388>
Object.const_set('Foo', c)  # => Foo

Although this isn’t very hard, there are two problems: (1) It’s a repetitive process that should be DRYed. (2) Callbacks that are invoked while the class is being created do not know the name of the class.

To understand the second problem, consider the following:

class Foo
  def self.inherited(base)
    puts "inherited class: #{base}, name: #{base.name}"
  end
end

When a class inherits from Foo, Ruby will invoke the inherited callback. For example,

c = Class.new(Foo)
# inherited class: #<Class:0x47fb92c>, name:
# => #<Class:0x47fb92c>

As you can see from output in this example, since the class has not yet been assigned to a constant, it is anonymous and does not yet have a name.

To address these issues, the functionality is encapsulated into a new method, Module#create. Since the method is defined in Module, it is also available to Class since Class inherits from Module.

Usage

Creating new classes/modules

Using the same example as before,

c = Class.create('Bar', :superclass => Foo)
# inherited class: Bar, name: Bar
# => Bar

As you can see, the name of the class is now available during the inherited callback and is automatically assigned to the ‘Bar’ constant in Object.

Specifying the parent class/module

In addition to specifying the superclass, you can also specify the parent module/class like so:

c = Class.create('Bar', :superclass => Foo, :parent => MyModule)
# inherited class: MyModule::Bar, name: MyModule::Bar
# => MyModule::Bar

Defining class/module methods

As you normally could when creating a new class, you can provide an additional block that defines the body of the class. For example,

c = Class.create('Bar', :superclass => Foo, :parent => MyModule) do
  def say_hello
    'hello'
  end
end
# inherited class: MyModule::Bar, name: MyModule::Bar
# => Bar
Bar.new.say_hello
# => "hello"

Dependencies

None.

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.