Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created December 12, 2013 20:20
Show Gist options
  • Save vanmichael/7934725 to your computer and use it in GitHub Desktop.
Save vanmichael/7934725 to your computer and use it in GitHub Desktop.
class Circle
attr_reader :radius
def initialize(radius)
@radius = radius
end
def diameter
@radius * 2
end
def circumference
(@radius * 2 * (Math::PI)).round(2)
end
def area
((@radius ** 2) * (Math::PI)).round(2)
end
end
require_relative '../../lib/circle'
describe Circle do
let(:circle) { Circle.new(5.0) }
it 'should have a radius' do
expect(circle.radius).to eql 5.0
end
it 'calculates the diameter' do
expect(circle.diameter).to eql 10.0
end
it 'calculates the circumference' do
expect(circle.circumference).to eql 31.42
end
it 'calculates the area' do
expect(circle.area).to eql 78.54
end
end
class Rectangle
attr_reader :length, :height
def initialize(length, height)
@length = length.to_f
@height = height.to_f
end
def perimeter
(@length * 2) + (@height * 2)
end
def area
@length * @height
end
end
require 'rspec'
require_relative '../../lib/rectangle'
describe Rectangle do
let(:rectangle) { Rectangle.new(10,5) }
it 'should have a length' do
expect(rectangle.length).to eql 10.0
end
it 'should have a height' do
expect(rectangle.height).to eql 5.0
end
it 'calculates the perimeter' do
expect(rectangle.perimeter).to eql 30.0
end
it 'calculates the area' do
expect(rectangle.area).to eql 50.0
end
end
class Square
attr_reader :side
def initialize(side)
@side = side.to_f
end
def perimeter
@side * 4
end
def area
@side ** 2
end
end
require 'rspec'
require_relative '../../lib/square'
describe Square do
let(:square) { Square.new(10.0) }
it 'should have a side' do
expect(square.side).to eql 10.0
end
it 'calculates the perimeter' do
expect(square.perimeter).to eql 40.0
end
it 'calculates the area' do
expect(square.area).to eql 100.0
end
end
class Trapezoid
attr_reader :a, :b, :c, :d, :height
def initialize(bottom_base,top_base,left_side,right_side,height)
@a = bottom_base.to_f
@b = top_base.to_f
@c = left_side.to_f
@d = right_side.to_f
@height = height.to_f
end
def perimeter
@a + @b + @c + @d
end
def area
0.5 * (@a+@b) * @height
end
end
require 'rspec'
require_relative '../../lib/trapezoid'
describe Trapezoid do
let(:trapezoid) { Trapezoid.new(289,25,129,135,129) }
it 'should have a bottom base' do
expect(trapezoid.a).to eql(289.0)
end
it 'should have a top base' do
expect(trapezoid.b).to eql(25.0)
end
it 'should have a left side' do
expect(trapezoid.c).to eql(129.0)
end
it 'should have a right side' do
expect(trapezoid.d).to eql(135.0)
end
it 'should have a height' do
expect(trapezoid.height).to eql(129.0)
end
it 'calculates the perimeter' do
expect(trapezoid.perimeter).to eql(578.0)
end
it 'calculates the area' do
expect(trapezoid.area).to eql (20253.0)
end
end
class Triangle
attr_reader :a, :b, :c
def initialize(a,b,c)
@a = a.to_f
@b = b.to_f
@c = c.to_f
end
def perimeter
@a +@b + @c
end
def half_perimeter
perimeter/2
end
def area
Math.sqrt(half_perimeter*(half_perimeter-@a)*(half_perimeter-@b)*(half_perimeter-@c)).round(2)
end
end
require_relative '../../lib/triangle'
describe Triangle do
let(:triangle) { Triangle.new(5,5,5) }
it 'should have an a side' do
expect(triangle.a).to eql(5.0)
end
it 'should have an b side' do
expect(triangle.b).to eql(5.0)
end
it 'should have an b side' do
expect(triangle.c).to eql(5.0)
end
it 'calculates an perimeter' do
expect(triangle.perimeter).to eql(15.0)
end
it 'calculates an area' do
expect(triangle.area).to eql(10.83)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment