Advertisements

On Wed, Jul 09, 2003 at 06:05:42AM +0900, Grzegorz Dostatni wrote:
> I need to do something silly. I need to access the code block as a
> variable (to pass it - without evaluating - to another function). How can
> I do that?

It's not silly at all, but the syntax isn't obvious if you haven't seen it
before.

At the end of your argument list give a named argument preceded with '&'.
This converts the block to an explicit Proc object which you can pass
around.

def function1(&blk)
function2(blk)
end

def function2(blk)
blk.call(99)
end

function1 { |i| puts i } #>> prints 99

> Is it possible for me to access the "text version of block?" Can I print
> out the code that has been attached to my function instead of executing
> it?

Grzegorz Dostatni wrote:
> Is it possible for me to access the "text version of block?" Can I print
> out the code that has been attached to my function instead of executing
> it?

As others have pointed out, this is not possible, atleast not without
jumping through hoops. If you don't mind jumping through hoops on the
caller side and a little uglier code, here is a quick stab I did at this
earlier:

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!