Ruby: Neat function uniq.size

My first Ruby program ever was to write a function to determine if a triangle given the size of its size, was scalene, isosceles or equilateral.

I wrote if statements to see if the sides were equals, but ruby can do it even neater:

case [a,b,c].uniq.size
when 1 then :equilateral
when 2 then :isosceles
else :scalene
end

Took a bit of self control to not use ‘;’ to end statements!

This entry was posted in Ruby and tagged . Bookmark the permalink.

Leave a comment