# Blocks and procs

**URL:** <https://www.ruby-forum.com/t/blocks-and-procs/97975>\
**Category:** Ruby\
**Created:** [July 11, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975 "2006-07-11T18:57:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/1 "2006-07-11T18:57:43Z")

</div>

Just curious, I’m just learning about Procs, and have already looked at  
using blocks.  
Are they very close in the way they function. It seems that way to me.  
Perhaps I’m wrong.  
Is one favoured above the other ?

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/2 "2006-07-11T19:01:14Z")

</div>

Hi –

On Wed, 12 Jul 2006, Dark A. wrote:

> Just curious, I’m just learning about Procs, and have already looked at  
> using blocks.  
> Are they very close in the way they function. It seems that way to me.  
> Perhaps I’m wrong.  
> Is one favoured above the other ?

They’re different things. A block is a syntactic construct, part of a  
method call. A Proc object is an object.

They are, however, closely related. Using special argument syntax, a  
method can turn the block supplied to it into a Proc:

def meth(&block)  
# block is the Proc version of the block  
block.call # etc.  
end

meth { # this is the block }

Also, you can use Proc objects as block substitutes:

meth &some\_proc\_object

David

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/3 "2006-07-11T19:23:29Z")

</div>

On Jul 11, 2006, at 11:55 AM, Dark A. wrote:

> Just curious, I’m just learning about Procs, and have already  
> looked at  
> using blocks.  
> Are they very close in the way they function. It seems that way to  
> me.  
> Perhaps I’m wrong.  
> Is one favoured above the other ?

I see David has already answered your question here, but just in case  
it helps I wrote about this on my blog a while back:

[http://blog.grayproductions.net/articles/2006/01/05/code-as-a-data-type](http://blog.grayproductions.net/articles/2006/01/05/code-as-a-data-type)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/4 "2006-07-11T20:18:45Z")

</div>

Good example. Also because it’s rails centric (something I’d eventually  
like to get to)  
I am running into some select errors though , via the select statement.  
If  
your up to reading it through (I don’t find any typos matched against  
your  
code), if not that is fine as the explanation helped me.

class ClientDB  
Record = Struct.new(:client\_name, :location, :projects)

def initialize  
@records = [ Record.new(“Gray Productions”, “Oklahoma”,  
[“Ruby Q.”, “Rails Extensions”] ),  
Record.new( “Serenity Crew”, “Deep Space”,  
[“Ship Enhancements”] ),  
Record.new( “Neo”, “Hollywood”,  
[“Rails interface for the Matrix”] ) ]  
end # end of method

```
def select( query )
  # parse query String
  rules = Hash[*query.split(/\s*AND\s*/).map do |rule|
    rule.split(/\s*=\s*/).map { |value| value.sub(/^['"](.+)['"]$/,

```

‘\1’) }  
end.flatten ]

```
  #match records
  @records.select { |record| block_given? and yield record }
    rules.all? { |field, value| record.send(field) == value }

end # end of method

```

end # end of class

require “pp”  
db = ClientDB.new  
pp db.select { |record| record.client\_name != “Gray Productions” }  
pp db.select { |record| record.client\_name +~ /crew/i }  
pp db.select { |record| record.projects.size == 1 }  
pp db.select { |record| record.projects.include? “Ruby Q.” }

* * *

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/5 "2006-07-11T20:21:54Z")

</div>

oops…this is the error:

rb:32:in `select’: wrong number of arguments (0 for 1) (ArgumentError)

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/6 "2006-07-11T20:59:12Z")

</div>

On Jul 11, 2006, at 1:18 PM, Dark A. wrote:

> I am running into some select errors though , via the select  
> statement.

Sure I see the trouble…

> require “pp”  
> db = ClientDB.new  
> pp db.select { |record| record.client\_name != “Gray Productions” }  
> pp db.select { |record| record.client\_name +~ /crew/i }  
> pp db.select { |record| record.projects.size == 1 }  
> pp db.select { |record| record.projects.include? “Ruby Q.” }

In order to run this example, you need to use the select() code in  
the article, just a paragraph above it. I’m slowly making changes  
and refining a solution as I go.

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/7 "2006-07-11T21:45:18Z")

</div>

On Jul 11, 2006, at 2:31 PM, Dark A. wrote:

> > ```
> > @records.select { |record| block_given? and yield record }
> > rules.all? { |field, value| record.send(field) == value }
> > 
> > end # end of method
> > 
> > ```
> > 
> > end # end of class

That’s from higher up in the article. Here’s the one just before the  
example you showed:

class ClientDB  
def select  
@records.select { |record| block\_given? and yield record }  
end  
end

Try that.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/8 "2006-07-11T21:33:05Z")

</div>

hmmmm…i have that code in there:

> ```
> end # end of method
> 
> ```
> 
> end # end of class

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 11, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/blocks-and-procs/97975/9 "2006-07-11T22:04:11Z")

</div>

Got it, had to remove the former query.  
Working now, on to lamda.

Stuart
