Skip to content

Instantly share code, notes, and snippets.

@bluemihai
Created May 5, 2016 18:49
Show Gist options
  • Save bluemihai/565f177587334886d5b034682eaec2f2 to your computer and use it in GitHub Desktop.
Save bluemihai/565f177587334886d5b034682eaec2f2 to your computer and use it in GitHub Desktop.
class House
attr_accessor :rooms, :furnished, :residents, :address, :price
def initialize(args={})
@rooms = args.fetch(:rooms, [])
@furnished = args.fetch(:furnished, true)
# THIS FAILS! REASON TO USE .fetch
# @furnished = args[:furnished] || true
end
end
describe House do
it '#initialize sets the right default/nil arguments' do
house = House.new
expect(house.rooms).to be_empty
expect(house.furnished).to eq true
empty_hash = {}
expect{ empty_hash.fetch(:foo) }.to raise_error(KeyError)
# THIS DOES NOT WORK! IT RAISES ERROR BEFORE WE LOOK FOR IT
# expect(empty_hash.fetch(:foo)).to raise_error(KeyError)
end
it '#initialize sets the right given arguments' do
house = House.new({
rooms: ['kitchen', 'bathroom', 'bedroom 1'],
furnished: false,
residents: 3,
address: '1024 Maple Lane',
price: 250000
})
puts "house is #{house.inspect}"
expect(house.rooms).to eq ['kitchen', 'bathroom', 'bedroom 1']
expect(house.furnished).to eq false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment