Skip to content

Add Method#source_code #18473

@dhh

Description

@dhh

Common pattern for me is to inspect the source code of a method I'm curious about like so:

irb(main):018:0> Recording.find(904).children.method(:load).source_location
  Recording Load (0.3ms)  SELECT  `recordings`.* FROM `recordings` WHERE `recordings`.`id` = 904 LIMIT 1
=> ["/Users/david/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/rails-03e672a3daec/activerecord/lib/active_record/relation.rb", 513]

Then copy the path to the clipboard, then go to the console and do mate path, then APPLE-G to jump to the line.

Let's make that simpler:

irb(main):018:0> puts Recording.find(904).children.method(:load).source_code
  Recording Load (0.3ms)  SELECT  `recordings`.* FROM `recordings` WHERE `recordings`.`id` = 904 LIMIT 1

    # Causes the records to be loaded from the database if they have not
    # been loaded already. You can use this if for some reason you need
    # to explicitly load some records before actually using them. The
    # return value is the relation itself, not the records.
    #
    #   Post.where(published: true).load # => #<ActiveRecord::Relation>
    def load
      exec_queries unless loaded?

      self
    end

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions