Skip to content

Instantly share code, notes, and snippets.

@jsuchal
Created February 17, 2015 14:42
Show Gist options
  • Save jsuchal/8df9f776c7ccac9f1161 to your computer and use it in GitHub Desktop.
Save jsuchal/8df9f776c7ccac9f1161 to your computer and use it in GitHub Desktop.
Painting = Struct.new(:name, :year)
def first(list, sorter = nil)
list = list.sort(&sorter) if sorter
list.first
end
def rest(list, sorter = nil)
list = list.sort(&sorter) if sorter
_, *rest = list
rest
end
describe Painting do
let(:one) { Painting.new("Spanish Couple In Front Of Inn", 1900) }
let(:two) { Painting.new("Guernica", 1937) }
let(:three) { Painting.new("Petite Fleurs", 1958) }
let(:paintings) { [one, two, three] }
let(:by_name_sorter) { ->(p1, p2) { p1.name <=> p2.name } }
it "should return the first element" do
expect(first(paintings)).to eq(one)
end
it "returns the rest of the elements" do
expect(rest(paintings)).to eq([two, three])
end
it "should return the first element with sorter" do
expect(first(paintings, by_name_sorter).name).to eq("Guernica")
end
it "returns the rest of the elements with sorter" do
expect(rest(paintings, by_name_sorter).map(&:name)).to eq([
"Petite Fleurs",
"Spanish Couple In Front Of Inn"
])
end
context 'sorter tests' do
it 'sorts by name' do
expect(by_name_sorter.call(one, two)).to eq(1)
expect(by_name_sorter.call(two, three)).to eq(-1)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment