Class: Class

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

Instance Method Summary collapse

Instance Method Details

#heir_moduleObject

Method #heir_module is not applicable to classes, raises TypeError.



22
23
24
# File 'lib/y_support/core_ext/class/misc.rb', line 22

def heir_module
  fail TypeError, "Method #heir_module is not applicable to classes!"
end

#parametrized_subclass(**parameters, &block) ⇒ Object Also known as: parametrize

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
17
# File 'lib/y_support/core_ext/class/misc.rb', line 9

def parametrized_subclass **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