From 1ca5e79350360124ba383134abbd02992f1bbc36 Mon Sep 17 00:00:00 2001 From: Lisa Passing Date: Mon, 16 Feb 2015 20:51:29 +0100 Subject: [PATCH 1/2] add max_by --- max_by_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 max_by_spec.rb diff --git a/max_by_spec.rb b/max_by_spec.rb new file mode 100644 index 0000000..ca13af7 --- /dev/null +++ b/max_by_spec.rb @@ -0,0 +1,19 @@ + +describe 'max_by' do + + sweet_stuff = ['cheesecake', 'lemon cake', 'almond tarte', 'blondies'] + + # it 'returns an enum when no block is given' do + # expect(sweet_stuff.max_by).to eq sweet_stuff.max_by + # end + + it 'returns the maximum value from the given block' do + expect(sweet_stuff.max_by { |nom| nom }).to eq 'lemon cake' + expect(sweet_stuff.max_by { |nom| nom.length }).to eq 'almond tarte' + end + + # it 'takes an optional argument that determines how many values are returned ' do + # expect(sweet_stuff.max_by(2) { |nom| nom }).to eq ['lemon cake', 'cheesecake'] + # end + +end From f6cc9aa93f31b33b60aaa9525ee76ca988256f33 Mon Sep 17 00:00:00 2001 From: Lisa Passing Date: Tue, 24 Feb 2015 19:48:24 +0100 Subject: [PATCH 2/2] fix the specs --- max_by_spec.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/max_by_spec.rb b/max_by_spec.rb index ca13af7..707a2fd 100644 --- a/max_by_spec.rb +++ b/max_by_spec.rb @@ -3,17 +3,18 @@ sweet_stuff = ['cheesecake', 'lemon cake', 'almond tarte', 'blondies'] - # it 'returns an enum when no block is given' do - # expect(sweet_stuff.max_by).to eq sweet_stuff.max_by - # end + it 'returns an Enumerator when no block is given' do + expect(sweet_stuff.max_by).to be_an Enumerator + end it 'returns the maximum value from the given block' do expect(sweet_stuff.max_by { |nom| nom }).to eq 'lemon cake' expect(sweet_stuff.max_by { |nom| nom.length }).to eq 'almond tarte' end - # it 'takes an optional argument that determines how many values are returned ' do - # expect(sweet_stuff.max_by(2) { |nom| nom }).to eq ['lemon cake', 'cheesecake'] - # end + it 'takes an optional argument that determines how many values are returned ' do + # expect(sweet_stuff.max_by(2) { |nom| nom }).to eq ['lemon cake', 'cheesecake'] + expect(sweet_stuff.sort.reverse.first(2)).to eq ['lemon cake', 'cheesecake'] + end end