From: Bill Clagett Date: 2002-09-21T09:51:16+09:00 Subject: Re: Creating a method given a block Sort of scary, but you could abuse ids: class Foo def build x, y, z, sym, &block instance_eval " class << self def #{sym.id2name} ObjectSpace._id2ref(#{block.id}).call end end " end end a = Foo.new a.build(1, 2, 3, :spam) { puts "printed from a block"} a.spam ----- Original Message ----- From: "Hal E. Fulton" To: "ruby-talk ML" Sent: Friday, September 20, 2002 5:00 PM Subject: Creating a method given a block > Suppose I have a method 'build' to which I am passing > (among other things) a Symbol and a block. > > The build method will define a method of the given > name and the block will in effect be the "body" > of that block. > > I've been messing with Procs trying to get this to > work. Just can't figure it out. > > Don't say "define_method" because I'm 1.6-compatible. > Don't know how that one works anyway. > > For the visual thinkers: > > def build(a, b, c, sym, &block) > #... > # Could use eval to define method > # referred to by 'sym'... but how > # to get it to call the block?? > end > > build(x, y, z, :get_busy) { do_stuff(); do_some_more() } > > Just curious. > > Hal > >