Skip to content

Instantly share code, notes, and snippets.

@tim-br
Created July 5, 2015 00:00
Show Gist options
  • Save tim-br/8d275ce265e1921d7c04 to your computer and use it in GitHub Desktop.
Save tim-br/8d275ce265e1921d7c04 to your computer and use it in GitHub Desktop.
### SETUP
require 'rspec'
RSpec.configure do |config|
config.color = true
end
### LOGIC (fix me)
# Returns the average of all the numbers in the array
def average(numbers)
unless numbers.nil? or numbers.empty?
numbers.map(&:to_f).reduce(0,:+) / numbers.size
end
end
### TEST CODE (don't touch me)
describe '#average' do
it 'returns nil for empty array' do
result = average([])
expect(result).to be_nil
end
it 'returns nil when nil is passed in' do
result = average(nil)
expect(result).to be_nil
end
it 'returns 4 for 3,4,5' do
result = average([3, 4, 5])
expect(result).to eq(4)
end
it 'can handle numbers represented as strings' do
result = average([10, '20', 30])
expect(result).to eq(20)
end
it 'can handle floats' do
result = average([1.0, 1.5, 2.0])
expect(result).to eq(1.5)
end
end
@Sailias
Copy link

Sailias commented Jul 6, 2015

separate these into separate files in the gist. makes them easier to read.

I guess this is just you playing around with rspec but here's a question.
what do you expect average([,,'','10']) to give you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment