Skip to content

Instantly share code, notes, and snippets.

@DonSchado
Created November 21, 2017 11:06
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 DonSchado/38495ae0e10597bc6d5916c5112ae91b to your computer and use it in GitHub Desktop.
Save DonSchado/38495ae0e10597bc6d5916c5112ae91b to your computer and use it in GitHub Desktop.
Railslove Ruby programming exercise A1
# Railslove Ruby programming exercise A1
#
# To run the following exercise you need to have rspec installed.
# Then watch the output of `rspec filter.rb` and see 3 of 4 failing specs.
#
# The task is to filter a given collection (L#31) for entries that contain unique pairs.
# While filtering return the same structure and keep the ranking.
#
# Write code in the body of the filter method to make the tests pass.
#
# Refactor until you're happy with your solution, by asking the follwing questions:
# * Is the code understandable/maintainable?
# * Do I really need all these if/else/elsif statements?
#
require 'ostruct'
class Collection < OpenStruct
def filter(a = nil, b = nil)
#
# your code goes here
#
collection
end
end
# don't touch me
require 'rspec'
RSpec.describe 'Collection' do
let(:all) do
[
{ 1 => %w(A B) },
{ 2 => %w(A C) },
{ 3 => %w(D E) },
{ 4 => %w(F G) },
{ 5 => %w(E A) },
{ 6 => %w(C H) },
{ 7 => %w(D B) }
]
end
subject(:collection) { Collection.new(collection: all) }
describe '#filter' do
it 'does not filter with empty params' do
expect(subject.filter).to eq(all)
end
it 'filters one result' do
expect(subject.filter('H')).to eq([{ 6 => %w(C H) }])
end
it 'filters many results' do
expect(subject.filter('A')).to eq([{ 1 => %w(A B) }, { 2 => %w(A C) }, { 5 => %w(E A) }])
end
it 'filters with 2 parameters' do
expect(subject.filter('A', 'C')).to eq([{ 2 => %w(A C) }])
end
it 'filters order independent' do
expect(subject.filter('C', 'A')).to eq([{ 2 => %w(A C) }])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment