From: sawadatsuyoshi@... Date: 2014-03-04T13:52:42+00:00 Subject: [ruby-core:61292] [ruby-trunk - Feature #5663] Combined map/select method Issue #5663 has been updated by Tsuyoshi Sawada. Also, regarding Yehuda Katz's concern: > The only caveat is that it would be impossible to intentionally return nil here; suggestions welcome. I would like to propose that the method takes an optional argument that determines what element is to be removed. By default, this is `nil`. [1, 2, 3, 4].partial_map{|i| i + 4 if i.even?} # => [6, 8] s = "abc" [0, 1, 2, 3, 4].partial_map(:ignore){|i| i.even? s[i] : :ignore} # => ["a", "c", nil] ---------------------------------------- Feature #5663: Combined map/select method https://bugs.ruby-lang.org/issues/5663#change-45623 * Author: Yehuda Katz * Status: Assigned * Priority: Normal * Assignee: Yukihiro Matsumoto * Category: lib * Target version: next minor ---------------------------------------- It is pretty common to want to map over an Enumerable, but only include the elements that match a particular filter. A common idiom is: enum.map { |i| i + 1 if i.even? }.compact It is of course also possible to do this with two calls: enum.select { |i| i.even? }.map { |i| i + 1 } Both cases are clumsy and require two iterations through the loop. I'd like to propose a combined method: enum.map_select { |i| i + 1 if i.even? } The only caveat is that it would be impossible to intentionally return nil here; suggestions welcome. The naming is also a strawman; feel free to propose something better. -- http://bugs.ruby-lang.org/