Created
August 4, 2014 01:22
-
-
Save kyuden/5f56bc14efe5f56b0979 to your computer and use it in GitHub Desktop.
Enumable条件検索系
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
Enumable#grep | |
レシーバと引数が===と一致するかどうかで絞り込む | |
# ===で一致するか、ということは==の条件に「範囲ないか」「インスタンス、クラスに属するか」「正規表現に一致するか」は加わったもの | |
(1..10).grep(2..3) #=> [2, 3] | |
%w|a 1 * @|.grep(/[1-9]/) #=> ["1"] | |
["a", 1, "&", "@"].grep(Integer) #=> [1] | |
grepの良い所は条件でしぼりこんだあとにブロックを渡して更に処理ができる | |
(1..10).grep(2..3){|i| i*2} #=> [4, 6] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment