From: Robert Klemme Date: 2011-12-11T22:20:05+09:00 Subject: Re: reading from file without end-of-lines On Sat, Dec 10, 2011 at 11:35 PM, Josh Cheek wrote: > 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. Josh, thanks for catching this in my example! > 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 Well, what you said in the sentence above is also "just convention". We could have #foo and #foo! with totally different behavior. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/