# Question on Procs

**URL:** <https://www.ruby-forum.com/t/question-on-procs/136258>\
**Category:** Ruby\
**Created:** [April 30, 2008, 6:30am UTC](https://www.ruby-forum.com/t/question-on-procs/136258 "2008-04-30T06:30:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 30, 2008, 6:30am UTC](https://www.ruby-forum.com/t/question-on-procs/136258/1 "2008-04-30T06:30:28Z")

</div>

What is the difference between this:

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def make\_filter(predicate, list)  
result = []  
list.each do |element|  
result \<\< element if predicate.call(element)  
end  
result  
end

require ‘rubygems’  
require ‘facets/integer/ordinal’

filter\_ths = make\_filter( lambda { |x| x.ordinal =~ /th$/ ? true :  
false }, list)

p filter\_ths

and this:

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def make\_filter(predicate)  
lambda do |list|  
result = []  
list.each do |element|  
result \<\< element if predicate.call(element)  
end  
result  
end  
end

require ‘rubygems’  
require ‘facets/integer/ordinal’

filter\_ths = make\_filter( lambda { |x| x.ordinal =~ /th$/ ? true :  
false })

p filter\_ths.call(list)

I am not clear on the advantage of returning proc object vs just the  
result as an array. TIA.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 30, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/question-on-procs/136258/2 "2008-04-30T12:21:00Z")

</div>

> What is the difference between this:

_snip_

> filter\_ths = make\_filter( lambda { |x| x.ordinal =~ /th$/ ? true :  
> false }, list)
> 
> p filter\_ths
> 
> and this:

_snip_

> filter\_ths = make\_filter( lambda { |x| x.ordinal =~ /th$/ ? true :  
> false })
> 
> p filter\_ths.call(list)
> 
> I am not clear on the advantage of returning proc object vs just the  
> result as an array. TIA.

The difference is abstraction, if I understand your question.

In the first case, you’ve filtered something (an enumerable type) and  
built a result array.

In the second case, you have built a little machine (the proc) that will  
take an enumerable type and filter it to give an array. This machine is  
probably a more reusable thing.

As a motivating example, lets say you have an object called Trawler (or  
a  
group of objects working together) that find interesting web pages to  
store. In the former case, Trawler will have to tell you about some web  
pages it’s found that might be interesting. You can filter them, and  
then  
tell it the ones you want it to store. In the latter case, you can give  
Trawler a filter, and it can then be autonimous - it can filter pages to  
see if they’re interesting.

🙂 Or have I totally missed the right end of your question?

You can write these more concisely like this (I’ve not tested this)…

cool\_pages = web\_pages.find\_all {|x| is\_this\_a\_cool\_page(x)}

verses…

page\_filter = proc {|array| array.find\_all {|x|  
is\_this\_a\_cool\_page?(x)}}  
cool\_pages = page\_filter.call(web\_pages)

Cheers,  
Benjohn

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2008, 12:05am UTC](https://www.ruby-forum.com/t/question-on-procs/136258/3 "2008-05-01T00:05:33Z")

</div>

On Apr 30, 12:26 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am not clear on the advantage of returning proc object vs just the  
> result as an array. TIA.

Hi Bala,

From the short example you provide, there really is no advantage to  
returning a Proc. However, if you needed to perform the same  
filtering operation in more than one place in your code, there might  
be an advantage to re-using the same filter, so that code does not  
have to be repeated.

Or alternatively, say you had an application that offered its users  
the ability to apply many commonly used filters (perhaps via a drop-  
down menu). Each of these filters might have an associated name, use  
example, and the Proc used to implement it. Well, name, example, and  
Proc could be members of a hash or alternatively instance variables in  
an instance of some Filter class.

As Benjohn pointed out, it’s a matter of abstraction. That Ruby  
allows one to store Procs in variables, have methods take Procs as  
parameters or return a Proc means Ruby has “first class functions”.  
You can read more about the concept here:

```
http://en.wikipedia.org/wiki/First-class_function

```

I suspect the code you showed us is someone’s attempt to demonstrate  
the power of Procs and first class functions. That kind of filtering  
would more commonly be done with Enumerable#select or  
Enumerable#reject. Since Arrays are Enumerable, the code would  
become:

```
list_2 = list_1.select { |item| item.ordinal =~ /th$/ }

```

(Also note that because the result of the =~ operator can be evaluated  
as true or false, the trinary conditional operator ?: is unnecessary.)

## Eric

Are you looking for on-site Ruby or Ruby on Rails training  
that’s been highly reviewed by former students?  
[http://LearnRuby.com](http://LearnRuby.com)
