Skip to content

Instantly share code, notes, and snippets.

@adambutler
Created April 18, 2017 14:14
Show Gist options
  • Save adambutler/6429ddcbe59382bffa72500686d6b042 to your computer and use it in GitHub Desktop.
Save adambutler/6429ddcbe59382bffa72500686d6b042 to your computer and use it in GitHub Desktop.
Adds position helper to Enumerable
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
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