UPDATE: After I wrote this, I started finding myself doing a lot of caller.grep(/(project_directory|suspected_gem)/)
to aid in debugging obscure interactions with internal gems and projects.
In looking at a pull request and noticing some awkward “first” and “last” iteration detection which also required each_with_index
, I started looking into what would be a cleaner way, and my first step was trying to figure out if there was an enumerable
context.
Ultimately I landed on Enumerable#grep
and Enumerable#grep_v
, which somewhat perplexed me. It’s not really a “grep” unless your collection’s values respond to that:
irb(main):056:0> 1000.times.map(&:to_s).grep(/00/)
=> ["100", "200", "300", "400", "500", "600", "700", "800", "900"]
Maybe you want to look for Class
es in the ObjectSpace
… The argument to #grep
is compared against an implicit element for each iteration with the ===
operator. So you could list everything in the ObjectSpace
that’s a Class
:
ObjectSpace.each_object.grep(Class) # too long to include here
One situation that I thought of that might be especially useful is Dir globbing:
irb(main):064:0> Dir['*'].grep(/(yarn|json)/)
=> ["package-lock.json", "package.json", "yarn.lock"]
If you were trying to the Ruby REPL as a shell, you could even:
irb(main):005:0> Dir['*/'].grep_v(%r{/packs/}).grep(/(.js$|.erb$|.rb$|.json$|.lock$)/)
=> ["app/controllers/application_controller.rb", "app/controllers/posts_controller.rb", "app/helpers/application_helper.rb", "app/helpers/posts_helper.rb", "app/javascript/src/jets/crud.js", "app/jobs/application_job.rb", "app/models/application_item.rb", "app/models/application_record.rb", "app/models/post.rb", "app/views/layouts/application.html.erb", "app/views/posts/edit.html.erb", "app/views/posts/index.html.erb", "app/views/posts/new.html.erb", "app/views/posts/show.html.erb", "app/views/posts/_form.html.erb", "babel.config.js", "config/application.rb", "config/environments/development.rb", "config/environments/production.rb", "config/environments/test.rb", "config/routes.rb", "config/webpack/development.js", "config/webpack/environment.js", "config/webpack/production.js", "config/webpack/test.js", "db/migrate/20210610023540_create_posts.rb", "db/schema.rb", "Gemfile.lock", "postcss.config.js", "spec/controllers/posts_controller_spec.rb", "spec/fixtures/payloads/posts-index.json", "spec/fixtures/payloads/posts-show.json", "spec/spec_helper.rb"]
And, as with .each
, .map
, etc… you can pass a block interact with each element. Your return from each iteration will map back to the output.
# array containing the contents of all the files matching:
Dir['*/'].grep_v(%r{/packs/}).grep(/(.js$|.erb$|.rb$|.json$|.lock$)/) { |f| File.open(f).read }
I’m not sure if I’ve seen any code that would be made cleaner by grep
(unless for utility scripting), and there’s always the risk of lowering maintainability of the code by using features no one else uses, but it’s nice to know that Ruby always has something more even after using it for many years.