# Get Proc of current block?

**URL:** <https://www.ruby-forum.com/t/get-proc-of-current-block/193954>\
**Category:** Ruby\
**Created:** [August 27, 2010, 3:43am UTC](https://www.ruby-forum.com/t/get-proc-of-current-block/193954 "2010-08-27T03:43:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 27, 2010, 3:43am UTC](https://www.ruby-forum.com/t/get-proc-of-current-block/193954/1 "2010-08-27T03:43:32Z")

</div>

Is there any way to get a hold of the current block? I’ve been playing  
around with recursive enumerations and it occurs to me that the most  
flexible technique would simply be to reuse the currently running  
block.

For example, where `this` would be the current block:

h = {:a=\>1, :b=\>{:c=\>3}}

h.map{ |k,v| Hash===v ? [k.to\_s, v.map(&this)] : [k.to\_s,v.to\_s] }

#=\> {“a”=\>“1”, “b”=\>{“c”=\>“3”}}

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 27, 2010, 7:28am UTC](https://www.ruby-forum.com/t/get-proc-of-current-block/193954/2 "2010-08-27T07:28:17Z")

</div>

On Fri, Aug 27, 2010 at 10:42 AM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Â #=\> {“a”=\>“1”, “b”=\>{“c”=\>“3”}}

That’s called the Y-combinator, something i wish could be added to  
core, but would be afraid of the many nay-sayers who are already  
confused by the current number of ways of creating blocks 🙂

def y(\*args, &block)  
y = lambda{|\*args| block.(\*args, &y) }  
end

p y{|n, acc, &b|  
n \< 2 ? acc : b.(n-1, n \* acc)  
}.(5, 1)

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 16, 2010, 10:12pm UTC](https://www.ruby-forum.com/t/get-proc-of-current-block/193954/3 "2010-09-16T22:12:15Z")

</div>

On Aug 26, 2010, at 6:42 PM, Intransition wrote:

> Is there any way to get a hold of the current block? I’ve been playing  
> around with recursive enumerations and it occurs to me that the most  
> flexible technique would simply be to reuse the currently running  
> block.

Limited, and pure evil:

lambda { |x| p x += 1; redo }[0]

Infinitely increments. `redo` lets you restart the current block, but  
you must manipulate the original args instead of passing 'em. Don’t do  
this. 🙂

~ j.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [September 27, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/get-proc-of-current-block/193954/4 "2010-09-27T16:22:21Z")

</div>

On Aug 27, 1:27 am, Michael F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }.(5, 1)

Helpful. Thanks. I am considering adding to Facets, but it would have  
to be called #Y b/c YAML defines #y.
