# Getting the source code of a block

**URL:** <https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238>\
**Category:** Ruby\
**Created:** [December 4, 2006, 12:22pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238 "2006-12-04T12:22:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 12:22pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/1 "2006-12-04T12:22:57Z")

</div>

Hello,

Is the following thing possible somehow?

def foo(&bar)  
do\_something\_with(bar)  
end

baz = foo { puts ‘hello’; i +=1; fluff(ork) }

now, baz should contain the string

“puts ‘hello’; i +=1; fluff(ork)”

since I need to do this in the same file where I am defining the block,  
I could get it using **FILE** + some regexps - just asking if there is  
some more elegant way…

TIA,  
Peter  
\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/2 "2006-12-04T12:41:00Z")

</div>

On 04.12.2006 12:20, Peter S. wrote:

> “puts ‘hello’; i +=1; fluff(ork)”
> 
> since I need to do this in the same file where I am defining the block,  
> I could get it using **FILE** + some regexps - just asking if there is  
> some more elegant way…

No. Alternatively you could cook your own

irb(main):001:0\> def pproc(code)  
irb(main):002:1\> block = eval(“lambda {#{code}}”)  
irb(main):003:1\> class \<\<block;self;end.send(:define\_method,  
:source\_code) { code }  
irb(main):004:1\> block  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> x = pproc ‘|a,b| a+b’  
=\> #Proc:0x003ba8bc@:1(eval)  
irb(main):007:0\> x[10,20]  
=\> 30  
irb(main):008:0\> x.source\_code  
=\> “|a,b| a+b”

Where do you need that for?

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/3 "2006-12-04T13:07:03Z")

</div>

> Where do you need that for?

I am setting up a complicated object (similar to a tree) with this code:

crap = define\_my\_object do  
foo do  
bar  
baz do  
fluff :something =\> :hairy  
ork :something =\> :other  
end  
end  
end

now, I would like to call a few methods on ‘crap’ which will alter the  
state of the object and eventually call a function which spits out crap  
as:

crap = define\_my\_object do  
foo do  
bar :some =\> :new\_param\_here  
baz do  
fluff :now =\> :here, :are =\> :some, :other =\> things  
ork :and =\> :even, :more =\> nil  
end  
end  
end

I have been thinking about some kind of serialization which would be  
certainly possible just by knowing crap - but since everything remains  
the same except a few parameters, it seemed easier to me to  
sub! (‘:something =\> :hairy’) {‘:now =\> :here, :are =\> :some, :other =\>  
things’} etc. - but for this I need the string of the original block of  
course.

bw,  
Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/4 "2006-12-04T14:11:12Z")

</div>

On 04.12.2006 13:06, Peter S. wrote:

> ```
> end
> fluff :now => :here, :are => :some, :other => things
> ork :and => :even, :more => nil
> end
> end
> end
> 
> ```

So you are modifying the state of an object and want to emit code that  
will recreate this state?

> I have been thinking about some kind of serialization which would be  
> certainly possible just by knowing crap - but since everything remains  
> the same except a few parameters, it seemed easier to me to  
> sub! (’:something =\> :hairy’) {’:now =\> :here, :are =\> :some, :other =\>  
> things’} etc. - but for this I need the string of the original block of  
> course.

I would rather go serialization - if you use YAML the serialized state  
is actually human readable (sort of). Marshal is binary but also faster  
IIRC. I would _definitively_ go serialization if you want to store  
configuration state between script executions.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/5 "2006-12-04T15:19:20Z")

</div>

> I would rather go serialization - if you use YAML the serialized state  
> is actually human readable (sort of). Marshal is binary but also faster  
> IIRC. I would _definitively_ go serialization if you want to store  
> configuration state between script executions.

Thanks for the answer. I guess to use yaml and maybe modify it a bit to  
be even more human readable could be a meaningful compromise.

Cheers,  
Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/6 "2006-12-04T15:19:21Z")

</div>

> [http://www.rubyquiz.com/quiz38.html](http://www.rubyquiz.com/quiz38.html)

Thanks! Nice 🙂

Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/7 "2006-12-04T19:54:30Z")

</div>

On Dec 4, 2006, at 03:20 , Peter S. wrote:

> now, baz should contain the string
> 
> “puts ‘hello’; i +=1; fluff(ork)”

$ cat x.rb  
require ‘rubygems’  
require ‘ruby2ruby’

puts proc { |hack| puts ‘hello’; i +=1; fluff(ork) }.to\_ruby

$ ruby x.rb  
proc { |hack|  
i  
puts(“hello”)  
i=(i + 1)  
fluff(ork)  
}

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 4, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/getting-the-source-code-of-a-block/78238/8 "2006-12-04T14:55:18Z")

</div>

On Dec 4, 2006, at 5:20 AM, Peter S. wrote:

> “puts ‘hello’; i +=1; fluff(ork)”  
> This was a Ruby Q. a while back:

[http://www.rubyquiz.com/quiz38.html](http://www.rubyquiz.com/quiz38.html)

James Edward G. II
