Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4454

Re: Scope problem (?) in implementing Design Patterns in Ruby

From David Jacobs <developer@wit.io>
Newsgroups comp.lang.ruby
Subject Re: Scope problem (?) in implementing Design Patterns in Ruby
Date 2011-05-12 20:56 -0500
Organization Service de news de lacave.net
Message-ID <C32E785734DD47739FFE9B46581FD36C@wit.io> (permalink)
References (1 earlier) <2ac328808edcaa956da4f286617acefb@ruby-forum.com> <4bfa1e5a3794daf70f5171094052dab0@ruby-forum.com> <9307dd9ab066746e74f5cbc1f8d02bbf@ruby-forum.com> <BANLkTin3iMtQZ7MiAfhG0Eg=XwnrfgbwGA@mail.gmail.com> <1a2fe490-3ad8-4c68-b1c2-2db31012a1a3@f31g2000pri.googlegroups.com>

Show all headers | View raw


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 that take 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 you all 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
On Thursday, 12 May 2011 at 12:00 pm, RichardOnRails wrote: 
> Hi 7Zip and David,
> 
> 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
> 

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-11 10:15 -0700
  Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 12:52 -0500
    Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 12:53 -0500
    Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 13:37 -0500
      Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-11 18:07 -0700
    Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-11 17:30 -0700
  Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 14:28 -0500
    Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 15:23 -0500
      Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 15:26 -0500
        Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 15:45 -0500
      Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 15:34 -0500
        Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 15:48 -0500
          Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-12 08:59 -0700
            Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-12 20:56 -0500
              Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 21:11 -0500
                Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-12 21:18 -0500
              Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-13 00:28 -0700
                Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-13 09:46 -0500
                Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-13 10:55 -0700
                Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-13 12:50 -0500
                Re: Scope problem (?) in implementing Design Patterns in Ruby Steve Klabnik <steve@steveklabnik.com> - 2011-05-13 13:14 -0500
                Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-23 19:24 -0700
              Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-24 20:33 -0700
                Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-25 17:30 -0500
                Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-27 22:08 -0700
  Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 21:20 -0500
    Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-12 21:26 -0500
  Re: Scope problem (?) in implementing Design Patterns in Ruby Ryan Davis <ryand-ruby@zenspider.com> - 2011-05-13 12:29 -0500

csiph-web