Class: Class

Inherits:
Object show all
Defined in:
lib/y_support/core_ext/class/misc.rb

Instance Method Summary collapse

Instance Method Details

#parametrize(parameters) ⇒ Object

Creates a subclass of the current class parametrized with a given set of parameters. The parameters have form { symbol: value } and they cause singleton method(s) named “symbol” be defined on the subclass, returning “value”.



9
10
11
12
13
14
15
16
# File 'lib/y_support/core_ext/class/misc.rb', line 9

def parametrize parameters
  Class.new( self ).tap do |subclass|
    parameters.each_pair { |symbol, value|
      subclass.define_singleton_method symbol do value end
    }
    subclass.define_singleton_method inspect do subclass.superclass.inspect + "<" end
  end
end