Skip to content

Instantly share code, notes, and snippets.

Created July 12, 2013 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5987878 to your computer and use it in GitHub Desktop.
Save anonymous/5987878 to your computer and use it in GitHub Desktop.
Paring an array of strings down to just those matching a given pattern is way faster with Array#grep
# big_array has over 120,000 strings in it
def select_search(q)
big_array.select do |s|
s =~ /^#{Regexp.esecap(q)}/
end
end
def grep_search(q)
big_array.grep( /^#{Regexp.escape(q)}/ )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment