Y
Yossef Mendelssohn
Actual code from a recent project:
# This is bunk, but I'm tired and can't think of the right way to
do this
# I was trying formats.detect, but that just returns the format,
not the result
result = nil
formats.each do |f|
result = f.process(mesg)
break if result
end
In this context, 'formats' is an array of format objects, each of
which represents a format a message can take. The process method takes
in a message, checks it against the format object's configuration, and
returns either nil (if there was no match) or some useful value.
Thanks to testing, my original concept of
formats.detect { |f| f.process(mesg) }
was rejected as being wrong. After all, I want the result of the
process call, not the format object. I could go with collect/detect,
but knowing I want the first makes that rather pointless, especially
if processing could be expensive and the array could be long.
Is there anything like Enumerable#detect_result? Is there any desire
for it? Is there any better name?
# This is bunk, but I'm tired and can't think of the right way to
do this
# I was trying formats.detect, but that just returns the format,
not the result
result = nil
formats.each do |f|
result = f.process(mesg)
break if result
end
In this context, 'formats' is an array of format objects, each of
which represents a format a message can take. The process method takes
in a message, checks it against the format object's configuration, and
returns either nil (if there was no match) or some useful value.
Thanks to testing, my original concept of
formats.detect { |f| f.process(mesg) }
was rejected as being wrong. After all, I want the result of the
process call, not the format object. I could go with collect/detect,
but knowing I want the first makes that rather pointless, especially
if processing could be expensive and the array could be long.
Is there anything like Enumerable#detect_result? Is there any desire
for it? Is there any better name?