From: SASADA Koichi Date: 2014-01-31T14:48:34+09:00 Subject: [ruby-core:60370] Re: [ruby-trunk - Feature #8987] map/collect extension which handles arguments (2013/10/06 0:26), sawa (Tsuyoshi Sawada) wrote: > [1, 2, 3, 4].map(&4.method(:+)) > # => [5, 6, 7, 8] Interesting. If we use �� (alias of lambda���, it is more short. module Kernel alias �� lambda end p [1, 2, 3, 4].map(&4.method(:+)) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��{|x| 4+x}) #=> [5, 6, 7, 8] If we define �� as the following definition, more short code. module Kernel def ��(a, sym) lambda{|x| a.send(sym, x)} end end p [1, 2, 3, 4].map(&��(4, :+)) #=> [5, 6, 7, 8] A bit shorter version. module Kernel def ��(expr) eval("lambda{|x| #{expr} x}") end end p [1, 2, 3, 4].map(&��("4+")) #=> [5, 6, 7, 8] If we have default parameter `_' (maybe matz doesn't like), we can make more short code. p [1, 2, 3, 4].map(&��{4+_}) #=> [5, 6, 7, 8] Summary: p [1, 2, 3, 4].map(&4.method(:+)) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��{|x| 4+x}) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��(4, :+)) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��("4+")) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��{4+_}) #=> [5, 6, 7, 8] (doesn't run) -- // SASADA Koichi at atdot dot net