# Proc objects and Maintainability - a design note to self

**URL:** <https://www.ruby-forum.com/t/proc-objects-and-maintainability-a-design-note-to-self/85515>\
**Category:** Ruby\
**Created:** [February 14, 2007, 5:51am UTC](https://www.ruby-forum.com/t/proc-objects-and-maintainability-a-design-note-to-self/85515 "2007-02-14T05:51:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 14, 2007, 5:51am UTC](https://www.ruby-forum.com/t/proc-objects-and-maintainability-a-design-note-to-self/85515/1 "2007-02-14T05:51:36Z")

</div>

So I use a couple of proc objects around my system.

Sort of nice in the sense that they can tell me where they were create  
(File/Line number)

Sort of nice in the sense that they parcel up a bit of lexical scope  
and allow me to use it elsewhen.

But I’m starting not to like them from a long term maintenance point  
of view.

They’re opaque. What was that scope that got parcelled? Tell me more  
about this proc instance? What is the responsibility of this  
particular instance? Pretty print what it knows so I can debug it…

So now I’m gently and quietly dropping them out of my system instead  
of doing something like…

proc\_obj = Proc.new{|a,b,c|  
doStuffWith(a,b,c,andD,andE,andFfromMyLexicalScope)}

I’m refactoring that to…

class SensibleNameProcObject  
def initialize( d, e, f)  
@d, @e, @f = d, e, f  
end

def to\_s  
“#{self.class}:The dowhop is #{@d} for egg #{@e} on fries #{@f}”  
end

def call( a, b, c)  
doStuffWith(a,b,c,@d, @e, @f)  
end  
end

proc\_obj = SensibleNameProcObj.new( andD,andE,andFfromMyLexicalScope)

More lines of code to define, but allows me more leeway to expand  
things, and thanks to duck typing, I don’t (unless I want to) have to  
change a single byte of client code.

Proc objects are a handy shorthand… but like many handy shorthands,  
sometimes in the long term you replace them with a long hand version  
for maintainability.

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 14, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/proc-objects-and-maintainability-a-design-note-to-self/85515/2 "2007-02-14T14:45:29Z")

</div>

John C.:

> ```
> "#{self.class}:The dowhop is #{@d} for egg #{@e} on fries #{@f}"
> 
> ```
> 
> end
> 
> def call( a, b, c)  
> doStuffWith(a,b,c,@d, @e, @f)  
> end  
> end

This makes me think that a Ruby programmer should maybe use the word  
Â»callÂ«  
in several cases where you would use Â»runÂ« or Â»startÂ« or Â»processÂ« in  
other languages. Provided that Â»callÂ« makes some sense in a specific  
case,  
it will line up beautifully with the core library. Â»alias [] callÂ« may  
come in handy, too.

Kalman

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 14, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/proc-objects-and-maintainability-a-design-note-to-self/85515/3 "2007-02-14T21:15:33Z")

</div>

On Feb 14, 2007, at 8:45 AM, Kalman N. wrote:

> This makes me think that a Ruby programmer should maybe use the  
> word »call«  
> in several cases where you would use »run« or »start« or »process« in  
> other languages. Provided that »call« makes some sense in a  
> specific case,  
> it will line up beautifully with the core library. »alias [] call« may  
> come in handy, too.

It might also make sense to define #to\_proc as in:

class Extra  
def initialize(i)  
@i = i  
end  
def call(a)  
a + @i  
end  
def to\_proc  
lambda {|a| call(a) }  
end  
end

add5 = Extra.new(5)

[1,2,3,4].map &add5 # [6,7,8,9]

Gary W.
