Mastodon

Finding methods in a Ruby codebase with Pry

Pry has been becoming a bigger and bigger part of my Ruby development workflow lately. Test not working? Pry. Debugging production from the console? Pry. Rake task doing something incomprehensible? Pry. But for finding stuff within a codebase my goto solution is still grep:

grep -ri def\ my_method .

Recently I grepped for a method called param_search_query in a project I am working on and came up with nothing.
After some head scratching and digging through some gems I thought I would try ctags.
Nothing. So I decided to see if Pry could help.

    30:   def has_query?
 => 31:     binding.pry
    32:     self.param_search_query && self.param_search_query.present? && self.param_search_query != "Eventname"
    33:   end
    34: 
    35:   def tickets
    36:     @tickets = Ticket.with_min_price
pry(#<Search:0x7fa5ea6f59e0>)> show-method self.param_search_query

It pry’s show-method function revealed the source of the confusion (pardon the pun), the method was being dynamically generated:

From: /home/mike/projects/frontend-server/app/models/search.rb @ line 20:
Number of lines: 7

define_method "param_#{psp}" do
  if self.search_params && self.search_params[psp].present?
    self.search_params[psp]
  elsif self.project
    self.project.get_param(psp)
  end
end

How did I get any work done without this?

Leave a Reply

Discover more from mikewilliamson.dev

Subscribe now to keep reading and get access to the full archive.

Continue reading