Created
October 6, 2017 13:26
-
-
Save bxt/5c1d9e52e178541a015381d65b449466 to your computer and use it in GitHub Desktop.
Map over ruby arrays / enumerables and have the last element marked with a boolean
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
def with_last(enumerable, length = enumerable.length) | |
Enumerator.new do |yielder| | |
enumerable.each_with_index do |element, index| | |
yielder.yield(element, index == length - 1) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment