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.

bill

···

----- Original Message -----
From: “Bill Clagett” bill@32768.com
To: ruby-talk@ruby-lang.org
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}
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” hal9000@hypermetrics.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
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