Last Updated: February 25, 2016
·
876
· blazeeboy

Checking multiple elements for equality in Ruby

while solving some programming challenges i faced the problem of checking multiple variables/expressions of equality, so the usual way of using == and "and" operators didn't like it so much.
here is my way

class Array
    def same?
        uniq.length==1
    end
end

x = 1
y = 2
z = 2
l = 2

# old way : x==y and y==z
puts [x,y,z].same? # false
# old way : y==z and z==l
puts [y,z,l].same? # true