正規表現のoオプションにRubyの暗黒面を見た

正規表現のoオプションにPerlの暗黒面を見た - はこべにっき ♨

実はRubyでも同じことが起こる。直訳してみた。

def match(pattern, text)
  # 繰り返しの中で使うのでoオプションをつけよう!
  return text =~ /^#{pattern}$/o;
end

# すべてマッチする
for $text in %w(vim vim vim vim vim)
    if (match('vim', $text)) 
        puts "match: vim";
    else 
        puts "not match: vim";
    end
end

# すべてマッチする?
for $text in %w(emacs emacs emacs emacs emacs)
    if (match('emacs', $text)) 
        puts "match: emacs";
    else 
        puts "not match: emacs";
    end
end
# >> match: vim
# >> match: vim
# >> match: vim
# >> match: vim
# >> match: vim
# >> not match: emacs
# >> not match: emacs
# >> not match: emacs
# >> not match: emacs
# >> not match: emacs