You can use multiple conditions to find a particular value in an array.
Example) From the numbers 1 to 10, derive the one that is 5 or less and is divided by 2. You will be able to solve problems like this.
Use Array # each and Array # select.
numbers = (1..10).to_a
rule = [["<", 5],["%", 2]]
rule.each do |b|
numbers.select! do |a|
if b[0] == "<"
a <= b[1]
else
a % b[1] == 0
end
end
end
p numbers # [2,4]
It is necessary to turn the condition (rule this time) with each and put the one you want to determine (number this time) in it. I used to be in trouble on the contrary, and recently I noticed this method, so I summarized it.
I would be happy if it could be a solution for someone who was stuck with the same problem.