Created
December 21, 2011 05:03
-
-
Save zacclark/1504662 to your computer and use it in GitHub Desktop.
Changing a friend's triangle implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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