while line = gets
next if line =~ /^\s*#/ # skip comments
break if line =~ /^END/ # stop at end
#substitute stuff in backticks and try again
redo if line.gsub!(/`(.*?)`/) { eval($1) }
end
What I don’t understand is this line:
line.gsub!(/`(.*?)`/) { eval($1) }
- What does the gsub! exactly do?
- the meaning of regex (.*?)
- the meaning of the block {eval($1)}
line, the result of the block.?modifies the.*RE so that it matches no more than is necessary to continue matching subsequent RE elements. This is called “non-greedy”. Without the?, the.*might also match the second backtick, depending on the rest of the line, and then the expression as a whole might fail.$1, which refers to the first paren-enclosed section (“backreference”) of the RE.In the big picture, the result of all this is that lines containing backtick-bracketed expressions have the part within the backticks (and the backticks) replaced with the result value of executing the contained Ruby expression. And since the outer block is subject to a
redo, the loop will immediately repeat without rerunning thewhilecondition. This means that the resulting expression is also subject to a backtick evaluation.