Answer the question
In order to leave comments, you need to log in
Why is Ruby doing the condition wrong?
In general, there is such code:
test_var = false
# тут начинается цикл do |item|
unless test_var
test_var = item.present?
end
# тут заканчивается цикл
test_var = false
array = []
# тут начинается цикл do |item|
unless test_var
test_var = item.present?
array.push(test_var)
end
# тут заканчивается цикл
render json: array
[false,true,false,false]
Answer the question
In order to leave comments, you need to log in
I assume (because there is only a piece of code in the question) that your do end loop does not have any termination condition before the entire collection is iterated over. That is, your loop is executed even after the completion of the unless block, which is why the values in the array are “not correct”.
One very smart person and a significant figure in the world of ruby once told me:
In Ruby, if not everything, then a lot can be done elegantly, and sometimes even in one line.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question