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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s