From: David Jacobs Date: 2011-05-13T23:46:38+09:00 Subject: Re: Scope problem (?) in implementing Design Patterns in Ruby --20cf301e31c3a91c6904a3290df2 Content-Type: text/plain; charset=ISO-8859-1 Hi Richard, The & has to be adjacent to "all" or "filename" to act as expected. The & can mean one of two related things when you pass it to a method: 1. It can mean "pass the lambda expression that this variable points to into a block". That makes the following to snippets equivalent: # Snippet 1 filter_function = lambda {|x| x == 2 } [1, 2, 3, 4, 2].select &filter_function # Snippet 2 [1, 2, 3, 4, 2].select {|x| x == 2 } 2. It can mean "pass the instance method named by a symbol, and treat it as a block". That makes these two equivalent: # Snippet 1 [1, 2, 3, 4, 2].select {|x| x.even? } # Snippet 2 [1, 2, 3, 4, 2].select(&:even?) So what my code is doing is calling the all method, for example, and that method's sole job is to return a lambda that can then be passed in per the above. You don't have to wrap the lambda in a method like I did. You could also just say: all = lambda {|x| x } I hope that makes things a little clearer! Cheers, David On Fri, May 13, 2011 at 3:31 AM, RichardOnRails < RichardDummyMailbox58407@uscomputergurus.com> wrote: > Hi Dave, > > I checked out your Version 2. Added puts stuff and restriction to > names of files, which should probably be another one of your methods > instead of my hard-coding. > Expanded code: http://www.pastie.org/1895917 > Output: http://www.pastie.org/1895932 > > I have only one question: I put a space between the & and the method > following it; the code broke. Is the construct you used documented on- > line somewhere? I've peeked at lambda documentation so I've got the > drift but I haven't used it in any of my code so far. > > I copied your memo on the approach and plan to review it and use/ > expand your code after I finish getting Russ Olson's code working or > giving up on it. To his credit, he posted his code on-line so I can > avoid typos and he included an apparently thorough set of unit tests. > So I've got a lot of studying to do. > > Again, thanks for your guidance and great ideas, > Richard > > --20cf301e31c3a91c6904a3290df2--