Grepping in Ruby
I was looking at a rather large model in Ruby and wanted to search through its methods to see what might be available. I found out there's an Enumerable#grep method that lets you search like you would in the terminal.
Here's a simple example:
["apple", "banana", "apricot"].grep(/^ap/)
# => ["apple", "apricot"]
Here's some more useful examples:
[1, "two", :three].grep(String)
# => ["two"]
[3, 7, 12].grep(1..10)
# => [3, 7]
Here's a real world example I used it for:
Post.methods.grep(/deleted/)
=> [:without_deleted, :with_deleted, :deleted, :only_deleted]