On Fri, Dec 9, 2011 at 7:27 PM, Gavin S. [email protected]
wrote:
values = File.foreach(“cl”).first(4).map :strip
What could be easier to understand than “send :strip to each element
of the collection”?
I disagree. &
is known to invoke to_proc
and place the result in the
block slot, and Symol#to_proc is known to be ->(obj) { obj.send self }
(or some equivalent). This is everywhere in Ruby, every method has a
block
slot, and you can put any block into it with the ampersand. The
map(:strip)
by contrast is it’s own pattern, the only other place I’ve
seen it is in sum = numbers.reduce(0, :+)
Also, it means the dev is disassociating themselves from the functional
paradigm, which means other relevant uses will be less mentally
accessible
e.g. filenames.map &File.method(:read)
On Sat, Dec 10, 2011 at 7:18 AM, Marc H. [email protected]
wrote:
It does not feel in-sync with the rest of the ruby code.
I do not share this opinion.
It is confusing for newcomers as well.
It is better that they learn about it than hide from it. Methods have a
magic block slot, Rubyists need to know this and know how to use it.
This makes me think of when I didn’t understand the $LOAD_PATH
,
because
Ruby <1.9.2 included “.” in it. Sure it made it easier for newbies like
me,
but it left a huge gap in my understanding that caused numerous problems
until I eventually figured it out when moving to latest rubies where I
had
to deal with it.
Just look at it again to compare:
I don’t think the &
is syntactic sugar. You could say a + b
is
syntactic sugar for a.+(b)
, but what is lines.map! &:strip!
syntactic
sugar for?
As an aside, this, brings up that this code is buggy. Since strip!
modifies
the string, there is no need for map. Especially considering that strip!
is
expected to be used by modifying the string, not relying on its return
value (which I think is much more egregious than using &
to access the
block slot), so it’s return values are not consistent:
lines = %W[line1\n line2]
lines.map! &:strip!
lines # => [“line1”, nil]
To avoid this bug, use lines.each &:strip!
Which brings up the point,
if
you change map such that you can lines.map! :strip!
then realize you
need
to use the each form, lines.each :strip
will not work. This is
inconsistent (and changing each
is impractical as it is implemented on
each collection rather than being inherited from Enumerable) but
&:strip
will, again, work everywhere.
(Btw, ruby complained about the lack of () in this example,
so the proper way would be:)
.map!(&:strip!)
I keep warnings off, I would write .each &:strip!
(or, more probably
.each &:chomp!
, given this particular use case)
I myself can live with that, but personally I’d rather explain
easier syntax to newcomers to ruby - they need to understand
both Symbols and Procs and methods ending in “!” when seeing
this. And I feel this is somewhat needless.
Methods ending in ! are exactly the same as methods that don’t end in !.
Any differences are just convention, and the conventions around it are
so
inconsistent that they’re practically meaningless. So this is like
saying
“they need to understand methods containing underscores” when talking
about
to_s