shmallen shmallen - 3 months ago 8
Ruby Question

About Ruby Flow Control in If/Elsif function?

Please bear with me as I am very new to learning Ruby. Not entirely sure how to word my question but its about the order in which I had written this code.

output = Hash.new
input.each do |word|
dictionary.each do |reference|
if (word.include? reference) && output[reference] = nil
output[reference] = 1
elsif word.include? reference
output[reference] += 1
end
end
end


The above code returns an error while the one below works the way I would like it to. Basically, my question is why does one work and not the other? Logically, I'm interpreting them as pretty much the same.

output = Hash.new
input.each do |word|
dictionary.each do |reference|
if (word.include? reference) && output[reference] != nil
output[reference] += 1
elsif word.include? reference
output[reference] = 1
end
end
end

Answer

Did you wrote an assignment:

output[reference] = nil

where you wanted to have a comparison?

output[reference] == nil