[Solved] What is wrong with my if statement ‘if x + y = z’


As @Pavan said in the comment, your conditional might be wrong. It should be == instead of =. = is the assignment.

if @weight.nominal + x == required ... end

Here what happens “behind the scene” with your original code:

  1. required is assigned to x.
  2. Add x to @weight.nominal (@weight.nominal + x)
  3. Evaluate the result from step 2, which is always true.
  4. Execute the code within if block, which is weights << @weight.id

Eventually, your x‘s value is lost, it will take required value instead and your conditional is useless since it always true.

solved What is wrong with my if statement ‘if x + y = z’