Skip to content

Instantly share code, notes, and snippets.

@zacclark
Created December 21, 2011 05:03
Show Gist options
  • Save zacclark/1504662 to your computer and use it in GitHub Desktop.
Save zacclark/1504662 to your computer and use it in GitHub Desktop.
Changing a friend's triangle implementation
# your way fixed
def triangle(a,b,c)
equal = false
[[a,b], [a,c], [b,c]].each do |first, second|
if first == second
equal ? (return :equilateral) : (equal = true)
end
end
if equal
return :isoceles
else
return :scalene
end
end
triangle(1,2,3)
# OO (and clearer) way
class Triangle
def initialize(a,b,c)
@a = a
@b = b
@c = c
end
def type
case [@a, @b, @c].uniq.length
when 1 then :equilateral
when 2 then :isosceles
when 3 then :scalene
end
end
end
puts Triangle.new(3,2,3).type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment