Created
April 18, 2017 14:14
-
-
Save adambutler/6429ddcbe59382bffa72500686d6b042 to your computer and use it in GitHub Desktop.
Adds position helper to Enumerable
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 each_with_position(&block) | |
each_with_index do |element, index| | |
first = index == 0 | |
last = index.equal?(size - 1) | |
block.call(element, first, last) | |
end | |
end | |
end |
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
RSpec.describe Enumerable do | |
context '#each_with_position' do | |
it 'returns bar' do | |
first_string = '' | |
last_string = '' | |
['a', 'b', 'c'].each_with_position do |string, first, last| | |
first_string = string if first | |
last_string = string if last | |
end | |
expect(first_string).to eq('a') | |
expect(last_string).to eq('c') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment