4 ms·
After reading the article, Discovered that instead of ["1","2","3"].map{|s| s.to_i} => [1, 2, 3] we can also do ["1","2","3"].map(&:to_i) => [1,
by gary4gar 14y ago
After reading the article, Discovered that instead of
["1","2","3"].map{|s| s.to_i}
=> [1, 2, 3]
we can also do
["1","2","3"].map(&:to_i)
=> [1, 2, 3]
This worked for me on Ruby 1.9 out of the box. And I think its an useful shortcut especially when its part of core ruby language & works WITHOUT using any external gems.
- Falling3 14y agoYes... which is exactly what the author said. Now try using a method that takes a parameter or chained method calls.
- esmevane 14y agoAlso, there's some application for lambdas, here: ruby-1.9.3-p0 :001 > double = -> x { x * 2 } => #<Proc:0x000001009236e0@(irb):1 (lambda)> ruby-1.9.3-p0 :002 > double[2] => 4 ruby-1.9.3-p0 :003 > [1].map(&double) => [2]