Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How is Proc object created from a block?

Thread Tools

How is Proc object created from a block?

Phil Tomson
Posts: n/a

So let's say you have a method that takes a block, like so:

def if?(cond, &b)
puts "b is a: #{b.class}"
if cond

call it:

if?(true) { puts "yes!" }

We know that "b is a: Proc" will be printed" (along with yes!) so we know that
when we pass a block into a method using the '&' that it 'magically' becomes a
Proc object.

But how exactly does that happen?

I tried modifying the Proc class to find out (thinking that must be
called at some point):

class Proc
alias_method ld_init, :initialize
def initialize &b
puts "Proc::b.class is: #{b.class}"
old_init &b
class << self
alias_method ld_new, :new
def new &b
puts "Proc::new"
old_new &b

if?(true) {puts "yes!"}
#=>b is a: Proc

It doesn't look like was called... So how was the block transformed
into a Proc object?

A bit of background: I'm trying to put some sort of @level class instance var
in Proc so I can determine how deeply nested in Proc's I am at any given

if?(cond){ #level==1
if?(cond2) { #level==2
if?(cond3) { #level==3

To do that I figured I'd need to do it in, but it doesn't seem to get


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
proc A def/calls proc B: variable scoping rules. NevilleDNZ Python 9 08-16-2006 04:36 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Convert VB.NET to TSQL PROC & Reference a Proc from another Proc David Lozzi ASP .Net 3 06-01-2005 06:35 PM
What is the diff btwn 'sho proc' and 'sho proc cpu' William J King Cisco 1 12-18-2003 11:50 PM