Skip to content

Instantly share code, notes, and snippets.

@kyuden
Created August 4, 2014 01:22
Show Gist options
  • Save kyuden/5f56bc14efe5f56b0979 to your computer and use it in GitHub Desktop.
Save kyuden/5f56bc14efe5f56b0979 to your computer and use it in GitHub Desktop.
Enumable条件検索系
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