Skip to content

Instantly share code, notes, and snippets.

@novikserg
Last active January 19, 2017 01:52
Show Gist options
  • Save novikserg/8033b09315d9ec1cb7a9f62b477c72af to your computer and use it in GitHub Desktop.
Save novikserg/8033b09315d9ec1cb7a9f62b477c72af to your computer and use it in GitHub Desktop.
class Triangle
attr_reader :a
attr_reader :b
attr_reader :c
def initialize(a, b, c)
@a, @b, @c = a, b, c
end
def type
return :impossible if sides.any? { |side| side == 0 }
case equal_sides_count
when 3 then :equilateral
when 2 then :isosceles
else :irregular
end
end
private
def sides
[a, b, c]
end
def equal_sides_count
sides.inject(Hash.new(0)) {|counts, side| counts[side] += 1; counts }.values.first
end
end
describe Triangle do
describe "#type" do
subject { triangle.type }
context "for impossible triangle" do
let(:triangle) { Triangle.new(1, 0, 1) }
it { is_expected.to be :impossible }
end
context "for equilateral triangle" do
let(:triangle) { Triangle.new(1, 1, 1) }
it { is_expected.to be :equilateral }
end
context "for isosceles triangle" do
let(:triangle) { Triangle.new(1, 2, 1) }
it { is_expected.to be :isosceles }
end
context "for irregular triangle" do
let(:triangle) { Triangle.new(1, 2, 3) }
it { is_expected.to be :irregular }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment