Created
May 3, 2009 17:14
-
-
Save tj/106063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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