Skip to content

Instantly share code, notes, and snippets.

@adeng21
Created March 8, 2014 18:06
Show Gist options
  • Select an option

  • Save adeng21/9436159 to your computer and use it in GitHub Desktop.

Select an option

Save adeng21/9436159 to your computer and use it in GitHub Desktop.
Week 3 Systems Check
class Square
attr_reader :side
def initialize(side)
@side = side
end
def area
side * side
end
def perimeter
side * 4
end
end
class Circle
attr_reader :radius
def initialize(radius)
@radius = radius
end
def diameter
radius * 2
end
def circumference
('%.5f' % (Math::PI * (radius * 2))).to_f
end
def area
('%.5f' % (Math::PI * (radius * radius))).to_f
end
end
class Rectangle
attr_reader :length, :width
def initialize(length, width)
@length = length
@width = width
end
def area
length * width
end
def perimeter
(2 * length) + (2 * width)
end
end
class RightTriangle
attr_reader :horizontal_leg, :vertical_leg
def initialize(horizontal_leg, vertical_leg)
@horizontal_leg = horizontal_leg
@vertical_leg = vertical_leg
end
def hypotenuse
Math.hypot(horizontal_leg, vertical_leg)
end
def perimeter
horizontal_leg + vertical_leg + hypotenuse
end
def area
(horizontal_leg * vertical_leg)/2
end
end
require 'rspec'
require_relative '../lib/geometry'
describe Square do
let(:square) {Square.new(8)}
it 'should take one argument - side' do
expect(square.side).to eq(8)
end
it 'calculates the area' do
expect(square.area).to eq(64)
end
it 'calculates the perimeter' do
expect(square.perimeter).to eq(32)
end
end
describe Circle do
let(:circle) {Circle.new(8)}
it 'should take one argument - radius' do
expect(circle.radius).to eq(8)
end
it 'calculates diameter' do
expect(circle.diameter).to eq(16)
end
it 'calculates circumference' do
expect(circle.circumference).to eq(50.26548)
end
it 'calculates area' do
expect(circle.area).to eq(201.06193)
end
end
describe Rectangle do
let(:rectangle) {Rectangle.new(6, 8)}
it 'should take two arguments - length and width' do
expect(rectangle.length).to eq(6)
expect(rectangle.width).to eq(8)
end
it 'calculates area' do
expect(rectangle.area).to eq(48)
end
it 'calculates perimeter' do
expect(rectangle.perimeter).to eq(28)
end
end
describe RightTriangle do
let(:right_triangle) {RightTriangle.new(3, 4)}
it 'should take two arguments - two sides' do
expect(right_triangle.horizontal_leg).to eq(3)
expect(right_triangle.vertical_leg).to eq(4)
end
it 'calculates length of hypotenuse' do
expect(right_triangle.hypotenuse).to eq(5)
end
it 'calculates perimeter' do
expect(right_triangle.perimeter).to eq(12)
end
it 'calculates area' do
expect(right_triangle.area).to eq(6)
end
end
@hchood
Copy link

hchood commented Mar 10, 2014

Nice job Albert!

A couple of suggestions:

Test possible errors that could arise. For example, what happens if someone passes in a negative number, or 0, as the side to Square.new?

For floats, use RSpec's be_within matcher. Instead of having to round off your return values (e.g. for circle's area & circumference), you can use Rspec's be_within matcher to hardcode in some float that the return value should be close to.

If you do want to round a number, you can use the round method instead of the sprintf method & then convert back to the string. (See circle circumference and area methods.)

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