红宝石的正则表达式有一些选项(例如i,x,m,o).i例如,意味着忽略大小写.
该o选项意味着什么?在ri Regexp,它表示只o执行#{}插值一次.但是当我这样做时:
a = 'one'
b = /#{a}/
a = 'two'
Run Code Online (Sandbox Code Playgroud)
b不改变(它停留/one/).我错过了什么?
Mar*_*der 19
/o导致#{...}特定正则表达式文字中的任何替换仅在第一次计算时执行一次.否则,每次文字生成Regexp对象时都将执行替换.
我也可以打开这个用法示例:
# avoid interpolating patterns like this if the pattern
# isn't going to change:
pattern = ARGV.shift
ARGF.each do |line|
print line if line =~ /#{pattern}/
end
# the above creates a new regex each iteration. Instead,
# use the /o modifier so the regex is compiled only once
pattern = ARGV.shift
ARGF.each do |line|
print line if line =~ /#{pattern}/o
end
Run Code Online (Sandbox Code Playgroud)
所以我想对于编译器而言,这是一个多次执行的单行.