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, &block) ⇒ 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”.



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

def parametrize **parameters, &block
  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
    subclass.class_exec &block if block
  end
end