[LRUG] ShRUG Golf update
Tom Stuart
tom at therye.org
Thu May 12 02:54:00 PDT 2011
On 12 May 2011, at 10:36, Jordi Noguera Leon wrote:
> 1. Enumerable#inject doesn't need a block, it actually takes 2 params, a initial value and a symbol as an argument. Each iteration the symbol is the method that'll be called on the accumulator and passed the current element. Also if you don't supply an initial element then it'll use the first element of the enumerable. So you can turn
>
> array.inject(0) {|s, i| s + i}
>
> into
>
> array.inject(:+)
>
> Into:
> array.inject :+
>
> :P
I can't make array.inject :+ work with no parens for some bizarre reason, while array.inject :* works fine. Anyone able to explain this?
It didn't turn out to be relevant, but I learnt that very often when using a symbol as a parameter you don't need to include even a space. Hence, if you're counting whitespace, you can do:
array.inject:*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20110512/dfd1fbfe/attachment-0003.html>
More information about the Chat
mailing list