Proc objects and Maintainability - a design note to self

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 : [email protected]
New Zealand

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs