Skip to content

Instantly share code, notes, and snippets.

@jmoon90
Created December 7, 2013 21:48
Show Gist options
  • Select an option

  • Save jmoon90/7849166 to your computer and use it in GitHub Desktop.

Select an option

Save jmoon90/7849166 to your computer and use it in GitHub Desktop.
class Circle
def initialize(radius)
@radius = radius
end
def perimeter
(Math::PI * @radius * 2).round(3)
end
def area
(Math::PI * @radius ** 2).round(3)
end
end
class Rectangle
def initialize(length, height)
@length = length
@height = height
end
def area
@length * @height
end
def perimeter
@length * 2 + @height * 2
end
end
require_relative 'shapes'
require_relative 'square'
require_relative 'rectangle'
require_relative 'circle'
require_relative 'triangle'
require_relative 'trapezoid'
require 'rspec'
describe Square do
it 'calculates the squares area' do
expect(Square.new(5).area).to eql(25)
end
it 'calculates the squares perimeter' do
expect(Square.new(5).perimeter).to eql(20)
end
end
describe Rectangle do
it 'calculates the rectangles area' do
expect(Rectangle.new(3,5).area).to eql(15)
end
it 'calculates the rectangles perimeter' do
expect(Rectangle.new(3,5).perimeter).to eql(16)
end
end
describe Circle do
it 'calculates the circles perimeter' do
expect(Circle.new(5).perimeter).to eql (31.416)
end
it 'calculates the circles area' do
expect(Circle.new(5).area).to eql(78.54)
end
end
describe Triangle do
equilateral = { side: 5, length:3, breadth: 4 }
sas = { angle: 28, length: 3, breadth: 4 }
it 'calculates the equilateral triangles perimeter' do
expect(Triangle.new(equilateral).perimeter).to eql(15)
end
it 'calculates the equilateral triangles area' do
expect(Triangle.new(equilateral).area).to eql(6)
end
it 'calculates the sas triangle area' do
expect(Triangle.new(sas).sas_area).to eql(1.62)
end
it 'calculates the sas triangle perimeter' do
expect(Triangle.new(sas).sas_perimeter).to eql(11)
end
end
describe Trapezoid do
perimeter_arguments = { base1: 5, base2: 15, base3: 7, base4: 10 }
area_arguments = { height: 10, base1: 5, base2: 8 }
it 'calculates the trapezoids perimeter' do
expect(Trapezoid.new(perimeter_arguments).perimeter).to eql(37)
end
it 'calculates the trapezoids area' do
expect(Trapezoid.new(area_arguments).area).to eql(65)
end
end
class Square
def initialize(side)
@side = side
end
def area
@side * @side
end
def perimeter
@side * 4
end
end
class Trapezoid
def initialize(arguments)
@height = arguments[:height]
@base1 = arguments[:base1]
@base2 = arguments[:base2]
@base3 = arguments[:base3]
@base4 = arguments[:base4]
end
def area
@height * (@base1 + @base2) / 2
end
def perimeter
@base1 + @base2 + @base3 + @base4
end
end
class Triangle
def initialize(arguments)
@side = arguments[:side]
@length = arguments[:length]
@breadth = arguments[:breadth]
@angle = arguments[:angle]
end
def perimeter
@side * 3
end
def area
@length * @breadth/2
end
def sas_area
0.5 * @length * @breadth * Math::sin(@angle).round(2)
end
def sas_perimeter
@length + @breadth * 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment