Skip to content

Instantly share code, notes, and snippets.

@tj
Created May 3, 2009 17:14
Show Gist options
  • Select an option

  • Save tj/106063 to your computer and use it in GitHub Desktop.

Select an option

Save tj/106063 to your computer and use it in GitHub Desktop.
str = 'this foo bar is fantastic i love foo'
words = []
str.split.each do |word|
words << word if word =~ /foo|bar/
end
p words
words = str.split.inject [] do |words, word|
words << word if word =~ /foo|bar/
words
end
p words
words = str.split.map do |word|
word if word =~ /foo|bar/
end.compact
p words
words = str.split.select do |word|
word if word =~ /foo|bar/
end
p words
p str.split.grep(/foo|bar/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment