From: John Date: 2002-07-14T16:41:28+09:00 Subject: Re: instance_eval "John" wrote in message news:_x2Y8.11504$Kx3.8405@newsread1.prod.itd.earthlink.net... > Hi all, > > I am trying to add a method dynamically. I can do the following just fine: > > def create > str = "def hello (*args) puts 'test' end" > self.instance_eval (str) > end > > This adds the hello method. But I want to pass a block to test so that it > executes the block instead of the "puts 'test'". > > I tried the following after searching some previous posts: > > def create (&block) > str = "def hello (*args) block.call end" > self.instance_eval (str) > end > > But to be honest I don't really know what I am doing there. ;-) I realize > that instance_eval can take a block, but what name would the method be? > > I will need to create multiple methods with different blocks, so I need them > to have different names. > > Anyone have any pointers or places I can look? > > TIA > John. > > > > So, without the define_method feature, I figured out, after re-reading page 47 of PR (for some bedtime reading), what I wasn't doing. What I needed to do was capture the block parameter into an instance variable, and make my newly created method call it. The block parameter is a proc object. I don't see a way to add a call to the block without storing the proc object into an intermediate variable in the object. John.