Creating a method given a block

Uh. No. Don’t do that. I should pay attention to those alarms going off in
the back of my head.

Nothing’s stopping the block from being gc’d.



----- Original Message -----
From: “Bill Clagett”
Sent: Friday, September 20, 2002 5:37 PM
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}

a =, 2, 3, :spam) { puts “printed from a block”}

----- 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??

build(x, y, z, :get_busy) { do_stuff(); do_some_more() }

Just curious.