Created
January 12, 2012 07:06
-
-
Save maraigue/1599144 to your computer and use it in GitHub Desktop.
[Ruby] Enumerable#grepの補集合
This file contains 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
require "./vgrep.rb" | |
p ["foo", "bar", "buz"].grep(/b/) # => ["bar", "buz"] | |
p ["foo", "bar", "buz"].vgrep(/b/) # => ["foo"] | |
p ["foo", "bar", "buz"].vgrep(/b/){ |x| x.upcase } # => ["FOO"] |
This file contains 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
module Enumerable | |
def vgrep(cond, &block) # 名前は "grep -v" より | |
result = select{ |x| !(cond === x) } # Ruby1.8.7以降ならtapで書くところなんだけど | |
result.map!(&block) if block | |
result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment