Skip to content

Instantly share code, notes, and snippets.

@fongfan999
Forked from robmiller/regex-o.rb
Created March 3, 2017 03:14
Show Gist options
  • Save fongfan999/9ff5f49e2364c24fb88eccec37973463 to your computer and use it in GitHub Desktop.
Save fongfan999/9ff5f49e2364c24fb88eccec37973463 to your computer and use it in GitHub Desktop.
Explaining Ruby regex's /o modifier
require "benchmark"
def letters
puts "letters() called"
sleep 0.5
"A-Za-z"
end
words = %w[the quick brown fox jumped over the lazy dog]
Benchmark.bm do |bm|
bm.report("without /o:\n") do
words.each do |word|
puts "Matches!" if word.match(/\A[#{letters}]+\z/)
end
end
bm.report("with /o:\n") do
words.each do |word|
puts "Matches!" if word.match(/\A[#{letters}]+\z/o)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment