Creating a method given a block

From: ts []

If you’re ‘eval’-averse, you could replace the class_eval with a


Gee, why did I think that Guy might have something to say here. ;^)

method_missing trap instead.

no :slight_smile:

pigeon% cat b.rb
class Extendable
def build(name, &block)
class << self; self; end.send(:define_method, name, &block)

Problem is Hal said he needed a solution that was 1.6-compatible,
therefore not relying on define_method…

Also, since the list of methods defined with ‘build’ is contained
in the keys of the @@dispatch_table, I don’t see why method_missing
would be objectionable here. The trap could still raise a
RuntimeError if the method that led to it were not one that was
initially defined with ‘build’…


  • jeff