# Accessing code blocks

**URL:** <https://rubytalk.org/t/accessing-code-blocks/6753>\
**Category:** ruby-talk\
**Created:** [8 July 2003 21:05 UTC](https://rubytalk.org/t/accessing-code-blocks/6753 "2003-07-08T21:05:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grzegorz\_Dostatni](https://avatars.discourse-cdn.com/v4/letter/g/9de053/32.png) [@Grzegorz\_Dostatni](https://rubytalk.org/u/Grzegorz_Dostatni)\
**Post date:** [8 July 2003 21:05 UTC](https://rubytalk.org/t/accessing-code-blocks/6753/1 "2003-07-08T21:05:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gennady](https://avatars.discourse-cdn.com/v4/letter/g/6bbea6/32.png) [@Gennady](https://rubytalk.org/u/Gennady)\
**Post date:** [8 July 2003 21:22 UTC](https://rubytalk.org/t/accessing-code-blocks/6753/2 "2003-07-08T21:22:34Z")

</div>

You can do:

block = proc { |arg|  
puts “Here’s the argument: #{arg}”  
}

def f(b,arg)  
b.call arg  
end

# proc can be passed around, like in:

f(block,1) # Here’s the argument: 1  
f(block,2) # Here’s the argument: 1

With this, you cannot print the code given in proc. If you really need  
to access the source of the code, keep the code in the string and eval  
to execute it (yack).

Gennady.

> **···**
>
> On Tuesday, July 8, 2003, at 02:05 PM, Grzegorz Dostatni wrote:
> 
> > 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

---

<div class="post-metadata">

**Author:** ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)\
**Post date:** [8 July 2003 21:28 UTC](https://rubytalk.org/t/accessing-code-blocks/6753/3 "2003-07-08T21:28:29Z")

</div>

Try this:

def foo(&bar) bar end

bar = foo { |x| x + 1 }

bar.call 1 # =\> 2

> **···**
>
> On 2003-07-09 06:05:42 +0900, Grzegorz Dostatni wrote:
> 
> > Please help.
> 
> –  
> 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

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [8 July 2003 23:03 UTC](https://rubytalk.org/t/accessing-code-blocks/6753/4 "2003-07-08T23:03:08Z")

</div>

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

> **···**
>
> On Wed, Jul 09, 2003 at 06:05:42AM +0900, Grzegorz Dostatni wrote:

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [9 July 2003 10:10 UTC](https://rubytalk.org/t/accessing-code-blocks/6753/5 "2003-07-09T10:10:18Z")

</div>

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/](http://www.pvv.org/~kentda/) ]**_/~  
> ))\_student\_/(( \__d L b_\_/ (pre-) Master of Science in Technology )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<div class="post-metadata">

**Author:** ![Brian\_Drummond](https://avatars.discourse-cdn.com/v4/letter/b/90ced4/32.png) [@Brian\_Drummond](https://rubytalk.org/u/Brian_Drummond)\
**Post date:** [9 July 2003 14:31 UTC](https://rubytalk.org/t/accessing-code-blocks/6753/6 "2003-07-09T14:31:45Z")

</div>

> You can do:
> 
> block = proc { |arg|  
> puts “Here’s the argument: #{arg}”  
> }  
> […]  
> With this, you cannot print the code given in proc. If you really need  
> to access the source of the code, keep the code in the string and eval  
> to execute it (yack).

Why not parse the string once, to produce a proc object? Then wrap the  
string and the proc together in another class, whose objects know _both_  
how to list themselves, and execute themselves, without the overhead of  
eval(source\_string). Modifying the string should of course re-parse the  
block…

As a Ruby nuby, I can’t yet put this into source, but I look forward to  
seeing how it can be done.

- Brian

> **···**
>
> On Wed, 9 Jul 2003 06:22:34 +0900, Gennady [bystr@mac.com](mailto:bystr@mac.com) wrote:
