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
Written by Emad Elsaid
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#