Hi Richard,
Happy to help.
I would definitely encourage you to not only look at design patterns (which are okay, but often overkill or already built in to Ruby) but also to Ruby's standard features to reduce code errors.
Ruby is a rich language. It has a lot of really well-crafted methods thattake care of a most of the tedium of coding, and for that reason the standard library is well worth learning.
For example, in your code, instead of importing 'find' and using it to look at directories, use Ruby's built-in Dir module. Dir['**/*'] will get youall subdirectories and files recursively.
Second, look into higher order functions. They let you change code from this:
def evaluate(dir, arg)
results = []
Dir['**/*'].each do |p|
next unless File.file? p
name = File.basename(p)
case arg
when String
results << p if File.fnmatch(@arg, name)
when Regexp
results << p if @arg.match(name)
end
end
results
end
Into this:
def match?(str_or_re, file)
str_or_re.is_a?(String) ? File.fnmatch(str_or_re, file) : str_or_re =~ file
end
def evaluate(dir, arg)
Dir['**/*'].select {|f| File.file?(f) and match?(arg, f) }
end
Any time you see this pattern ...
my_temporary_variable = []
my_collection.each do |elem|
my_temporary_variable << elem if some_condition
end
return my_temporary_variable
.. you know that higher order functions would probably be a better solution.
Hope that helps, and good luck learning this beautiful language.
Cheers,
David
Any time you see this pattern in your code, you should automatically think
Thanks very much to you both for hanging in there with me.
I hacked up a working version (agnostic for 1.8.6/1.9.2) here:
http://www.pastie.org/1893405,
with results here:
http://www.pastie.org/1893420
I apologize for having screwed a Pastie URL last time.
I left comments in code for the hacks I made. When I first
encountered the problem, I made a feeble attempt at prefixing :: for
All but I failed to take note of the fact that the All definition was
subordinate to Expression. That was stupid on my part, but I blame it
on the fact that I'm 77, have been retired from programming for 7
years and have only got serious about learning Ruby/Rails about a year
ago. It's been a struggle; I used to be faster on the uptake.
BTW, David Black has a very nice exposition of lookup for methods and
classes on page 98 et seq of his "The Well-Grounded Rubyist", one of
my "bibles".
I don't know how my hacks will playout for the parsing scheme in
"Design Patterns in Ruby". I going to follow it until this newsgroup
and/or I discover it's a fool's errand.
With my very best wishes to you both and thanks for your generous
insights,
Richard