Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Accessing code blocks (http://www.velocityreviews.com/forums/t808651-accessing-code-blocks.html)

Grzegorz Dostatni 07-08-2003 08:46 PM

Accessing code blocks
 

Cheers!

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?

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?

The only functions that work on code block (that I could find) are:
block_given? and yield. Obviously they do not fit the bill...

Please help.

Grzegorz Dostatni



Florian Frank 07-08-2003 09:28 PM

Re: Accessing code blocks
 
On 2003-07-09 06:05:42 +0900, Grzegorz Dostatni wrote:
> Please help.


Try this:

def foo(&bar) bar end

bar = foo { |x| x + 1 }

bar.call 1 # => 2


--
5) "I have challenged the entire quality assurance team to a Bat-Leth
contest. They will not concern us again."
-- Top 12 things likely to be overheard if you had a Klingon Programmer


Brian Candler 07-08-2003 11:03 PM

Re: Accessing code blocks
 
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?


No, decompiling blocks is not possible AFAIK :-)

Regards,

Brian.


Kent Dahl 07-09-2003 09:56 AM

Re: Accessing code blocks
 
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:

class X
def first_definition( &binding )
@binding ||= binding
source = binding.call
redefine( source )
end
def redefine( source )
@source = source
@block = eval "Proc.new { #{source} }", @binding
end
def x(i)
@block.call(i)
end
end
x = X.new
a = 3
x.first_definition { %{|i|a+i} }
puts x.x(2) #=> 5
x.redefine %{|i|a-i}
puts x.x(2) #=> 1

Notice the syntax { %{|i|a+i} }
I wrap a string, "|i|a+i", inside a block. The block at this level only
serves as a binding, the contents of the string is used to create the
block itself. HTH

--
(\[ Kent Dahl ]/)_ _~_ _____[ http://www.pvv.org/~kentda/ ]_____/~
))\_student_/(( \__d L b__/ (pre-) Master of Science in Technology )
( \__\_õ|õ_/__/ ) _)Industrial economics and technological management(
\____/_ö_\____/ (____engineering.discipline_=_Computer::Technology ___)


All times are GMT. The time now is 07:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.