Last active
December 18, 2015 18:29
-
-
Save bryanwoods/5826124 to your computer and use it in GitHub Desktop.
Enumerable#compact_flat_map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enumerable | |
def compact_flat_map(&block) | |
return to_enum(:compact_flat_map) unless block_given? | |
compact.flat_map(&block) | |
end | |
alias_method :compact_collect_concat, :compact_flat_map | |
end | |
p [1, 2, nil, 4, 5, nil].compact_flat_map { |i| [i + 1, i - 1] } | |
["Bryan", "Baldur", nil, nil, "DAN"].compact_flat_map do |name| | |
p "I'M #{name.upcase} AND I'M YELLING" | |
end | |
__END__ | |
[2, 0, 3, 1, 5, 3, 6, 4] | |
"I'M BRYAN AND I'M YELLING" | |
"I'M BALDUR AND I'M YELLING" | |
"I'M DAN AND I'M YELLING" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment