Really, really basic block in Ruby:
list = (1..125).to_a
list.each do |x|
print x if 125 % x = 0
end
Gives me:
ZeroDivisionError: divided by 0
from (irb):3:in `%'
from (irb):3
from (irb):2:in `each'
from (irb):2
Yet the block “puts x” returns 1 through 125, just like it should.
What is going on?
You’re assigning 0 to x before the
%operator is evaluated:Note that the last bit,
x = 0, is assignment. You need to use==to test for equality: