IRB Rocks!

# Hack your code

Assignments in conditionals

Assignments in conditionals

In Ruby, we can assign a value to a variable in a conditional. For example:

if v = foo
  puts v
end

When we write this code, Rubocop show us a warning: Assignment in condition - you probably meant to use ==. This is not an error and it's perfect valid in Ruby, but Rubocop tells you that it could be an error because v = foo is pretty similar to v == foo.

To pass Rubocop checks and prevent mistakes, you can write the following valid code:

if (v = foo)
  puts v
end

gem share .gem