# Creating a method given a block

**URL:** <https://rubytalk.org/t/creating-a-method-given-a-block/1943>\
**Category:** ruby-talk\
**Created:** [23 September 2002 19:51 UTC](https://rubytalk.org/t/creating-a-method-given-a-block/1943 "2002-09-23T19:51:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gray\_Jeff](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Gray\_Jeff](https://rubytalk.org/u/Gray_Jeff)\
**Post date:** [23 September 2002 19:51 UTC](https://rubytalk.org/t/creating-a-method-given-a-block/1943/1 "2002-09-23T19:51:27Z")

</div>

> From: ts [[mailto:decoux@moulon.inra.fr](mailto:decoux@moulon.inra.fr)]

> > If you’re ‘eval’-averse, you could replace the class\_eval with a
> 
> yes,

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

> > method\_missing trap instead.
> 
> no 🙂
> 
> pigeon% cat b.rb  
> #!/usr/bin/ruby  
> class Extendable  
> def build(name, &block)  
> class \<\< self; self; end.send(:define\_method, name, &block)  
> end  
> end

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’…

Cheers,

- jeff
