Skip to content

Instantly share code, notes, and snippets.

@portableworld
Created September 17, 2012 19:29
Show Gist options
  • Save portableworld/3739271 to your computer and use it in GitHub Desktop.
Save portableworld/3739271 to your computer and use it in GitHub Desktop.
Adding Array#average with tests
class Array
def not_empty?
!self.empty?
end
def average
temp = self.compact
temp.map! {|e| e.is_a?(String) && (e.match(/\d/)) ? e.to_f : e}
return nil if (temp.not_empty?) && (temp.reject! {|e| !e.is_a? Numeric}; temp.empty?)
return 0.0 if temp.empty?
temp.inject(:+) / temp.length.to_f
end
end
#Tests
describe Array do
describe "#not_empty" do
it 'should return true for an Array with more than zero elements' do
[1,2].not_empty?.should be_true
end
it 'should return false for an Array with zero elements' do
[].not_empty?.should be_false
end
end
describe "#average" do
it 'should return 5 for [1..9]' do
(1..9).to_a.average.should eq(5)
end
it 'should return 5.5 for [1..10]' do
(1..10).to_a.average.should eq(5.5)
end
it 'should return 0.0 for [nil]' do
[nil].average.should eq(0.0)
end
it 'should return 0.0 for []' do
[].average.should eq(0.0)
end
it 'should not alter original Array' do
a = [1, 2, 3, nil, 5, nil]
original_length = a.length
a.average
a.length.should eq(original_length)
end
it 'should return nil for [String, String, String]' do
%W(Nothing but Strings).average.should be_nil
end
it 'should return 5 for [1..9, String, String]' do
a = (1..9).to_a
a += %W(Some added strings)
a.average.should eq(5)
end
it 'should return 5 for ["1".."9"]' do
('1'..'9').to_a.average.should eq(5)
end
it 'should return 5 for ["1".."9", String, String]' do
a = ('1'..'9').to_a
a += %W(Some added strings)
a.average.should eq(5)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment