Hendrik Kleine Hendrik Kleine - 5 months ago 9
Ruby Question

Ruby - Loop IF x times and change condition

I would like to loop 20 times over an if statement, but each time it's run, some things must change.

Example:

input = [0,0,0,44,754,22,0,632,2,22,0,2,nil,2,24,nil,666,90909,2,4,6,7,2,7,3,2,2,7,1,8,6,3,2,19,5,46]

Statement = "Statement THIS"

if
input[1] != nil &&
input[2] == 0
Statement.sub! "THIS", "WHERE #{input[8]} #{input[9]} THIS"
else
end

puts Statement #Statement WHERE 2 22 THIS

if
input[5] != nil &&
input[6] == 0
Statement.sub! "THIS", "AND #{input[12]} #{input[13]} THIS"
else
end

puts Statement #Statement WHERE 2 22 AND 2 THIS

if
input[9] != nil &&
input[10] == 0
Statement.sub! "THIS", "AND #{input[16]} #{input[17]} THIS"
else
end

puts Statement #Statement WHERE 2 22 AND 2 AND 666 90909 THIS


In the second IF statement the following things have changed:


  1. Each key has increased by 4 (1,2,8,9 became 5,6,12,13)

  2. The word WHERE has changed to AND



I would like this behaviour to repeat another 18 times, so the third IF statement has:


  1. Each key has increased by 4 (5,6,12,13 became 9,10,16,17)

  2. The word HELLO has changed to GOODBYE (however this rule is now redundant, since the second IF statement took care of it).


Answer
input = [
  0,0,0,44,754,22,0,632,2,22,0,2,
  nil,2,24,nil,666,90909,2,4,6,7,
  2,7,3,2,2,7,1,8,6,3,2,19,5,46
]

(1..Float::INFINITY).step(4) do |i|
  i = i.to_i # is float here
  break Statement if i >= input.size
  next if input[i].nil? || !input[i+1].zero?

  keyword = Statement =~ /WHERE/ ? 'AND' : 'WHERE'
  Statement.sub! "THIS", "#{keyword} #{input[i+7]} #{input[i+8]} THIS"
end
#⇒ "Statement WHERE 2 22 AND  2 AND 666 90909 THIS"
Comments