Q:

Ensure that multiple variables have a different value

I have to compare multiple variables and check if they are different. Is there any smooth way to do this? I can do
var1 = 3
var2 = "test"
var3 = 100

if var1 != var2 && var1 != var3 && var2 != var3
  ...
end

But if the list of variables are getting longer, this becomes unhandy. I thought about something like var1 != var2 != var3 but that doesn't work. So maybe there is a simple Ruby-way to do this.

A:

One way might be using Array#uniq.
inputs = [var1, var2, var3]

if inputs.uniq == inputs
  # ...
end

# or

if inputs.uniq.length == inputs.length
  # ...
end

A:

You can use uniq's destructive counterpart uniq! to determine if an array contains duplicates. It returns nil if no duplicates were found:
if ![var1, var2, var3].uniq!
  # no duplicates
end

