Skip to content

Instantly share code, notes, and snippets.

@fgarcia
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgarcia/189e2dc221fc51084780 to your computer and use it in GitHub Desktop.
Save fgarcia/189e2dc221fc51084780 to your computer and use it in GitHub Desktop.
generic array adapter
class ArrayAdapter
def initialize(array, &block)
raise 'Adapter is useless without a block' unless block_given?
@raw = array
@transformer = block
end
def each(&block)
# StackOverflow
# http://stackoverflow.com/q/27798855/253098
@raw.map { |raw| @transformer.call(raw) }.each(&block)
end
end
describe '#each' do
it 'applies transformation block' do
old = [1, 2, 3]
adapted = ArrayAdapter.new old do |v|
v.to_s
end
adapted.each do |v|
expect(v).to be_kind_of String
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment