Scope problem (?) in implementing Design Patterns in Ruby

RichardOnRails wrote in post #998460:

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.

Your bible, “The Well-Grounded Rubyist”, uses the &:meth_name construct
frequently. Unfortunately, David Black only explains that syntax after
using it many times. David Black says that the ‘&’ in ‘&:meth_name’
does two things:

  1. The ‘&’ calls the specified object’s to_proc() method. And
    Symbol#to_proc is defined something like this:

class Symbol
def to_proc
proc{|obj| obj.send(self)} #self=:meth_name
end
end

That just creates a function that takes an object as an argument and
sends() the :meth_name to the object, i.e. the function causes the
:meth_name method to be called on the object.

  1. The ‘&’ also tells ruby to convert the Proc to a block, just as if
    you had written:

… do |element|
element.send(:meth_name)
end

after the method call that is using the &:meth_name argument.

In my opinion, that syntax is a step backwards for ruby. It makes ruby
more perl like, i.e. it’s right down perl’s alley of producing write
only code(i.e. no one can read it!).

On May 11, 2011, at 10:20 , RichardOnRails wrote:

I’m hoping that employing “Design Patterns in Ruby” will lead to less
coding errors and more easily maintained code. I’m stuck with the Not
pattern in the File Finding pattern in Chapter 15.

Design patterns do not lead to less coding errors nor more easily
maintained code.

Design patterns simply provide developers with a shared vocabulary to
oft-used structures. It allows them to more easily communicate. If
you’re coding in a vacuum it won’t help (from that perspective).

Not to say that it isn’t worthwhile to read more code. That’s always
worthwhile.

  1. It calls the specified object’s to_proc() method. And Symbol#to_proc
    is defined like this:

Small note, and some history:

This was originally introduced in ActiveSupport, where that was in fact
the
definition. But performance was terrible. It was added to 1.8.7 & 1.9,
but
is implemented in C, and is a bit more efficient. The semantics are the
same
though.

On May 13, 10:46am, David J. [email protected] wrote:

Snippet 1

Output:http://www.pastie.org/1895932
So I’ve got a lot of studying to do.

Again, thanks for your guidance and great ideas,
Richard

Hi again, Dave,

I hope that makes things a little clearer!

Crystal! as Kaffee said to Col. Jessep during their Gitmo meeting (A
Few Good Men)

Thanks for illuminating another area of Ruby where my proficiency
needs enhancement :slight_smile:

Best,
Richard

On May 12, 9:56pm, David J. [email protected] wrote:

Second, look into higher order functions. They let you change code from this:

Hi Dave,

I just want to let you know your tutelage on this thread has not gone
in vain.

  1. I wanted to return a list (i.e. array) of filenames rather than
    concatenated strings. Maybe parsing the concatenated strings would
    have gotten me my lists, but I want the arrays to be the direct output
    (in part just to see if I understood your code well enough to do it.)

  2. I wanted to do it by passing a search pattern to Filename.new and
    a directory to the related expression.

These changes are displayed at http://www.pastie.org/1969301

I’m continuing to work on embracing the higher-order methods you
advocated.

Best wishes,
Richard

On May 13, 1:50pm, 7stud – [email protected] wrote:

lot, and David Black says that the ‘&’ in ‘&:meth_name’ does two things:

That just creates a function that takes an object as an argument and
sends() the :meth_name to the object, i.e. the function calls the
:meth_name method on the object.

  1. The ‘&’ tells ruby to convert the Proc to a block.


Posted viahttp://www.ruby-forum.com/.

Hi 7stud,

I just your the response you posted a week of two ago, for which I am
grateful

Your bible, “The Well-Grounded Rubyist” uses the &:meth_name construct a
lot, and David Black says that…

I had looked at the indices of several books for a reference to the
prefix &, to no avail. So I abandoned that idea and thus never
checked Black’s book.

Thanks to your note, I see that Black has indexed two dense sections
related to the prefixed &, so I’ve got them on my To-Do list for
study.

Thanks again for taking the trouble to directing me to Black’s
coverage.

Best wishes,
Richard

Hi Richard,

Glad the advice helped!

Probably a more concise (and robust) way to get the full pathname of
your Dir[] results would be:

full_name = File.expand_path(path)

This has the advantage of expanding ‘~’ to ‘/home/richard’ (or
whatever).

If you add in higher order functions here (I’m just saying this to
reinforce the earlier concept along with the above method), you go from
this:

array = []
Dir.chdir(dir) do
Dir[’**/*’].each do |path| # Added each & block
full_name = File.join(dir, path)
array << path
end
end
array

To this:

Dir.chdir(dir) do
Dir[’**/*’].map {|path| File.expand_path(path) }
end
On Tuesday, 24 May 2011 at 11:35 pm, RichardOnRails wrote:

On May 25, 6:30pm, David J. [email protected] wrote:

If you add in higher order functions here (I’m just saying this to reinforce
the earlier concept along with the above method), you go from this:
To this:

I’m continuing to work on embracing the higher-order methods you
advocated.

Best wishes,
Richard

Hi David,

Thanks for your additional improvement. Shrinking 8 lines down to 3
reduces the likelihood of error 62.5% I’d say. I like those odds|

I’m presently going through David Black’s treatise as fast as I can
internalize the stuff. Specifically, I just found out the the number
of Enumerable methods went from 26 to 47 as I switched my WindowsXP-
Pro’s PATH from Ruby 1.8.6 to 1.9.2. I just ran into a minor problem
that I’m going to post on a separate thread because I don’t want
presume to hang on your coat-tails.

Best wishes,
Richard