Rails 5 adds method source

Here is an example of the Rails team making it easier for developers to access much needed tools: you can now inspect the source code for a method right from irb. Say you had the following source code:

class Flower < ApplicationRecord
  # opens the flower
  def bud
    puts 'budding'
  end
end

In irb you can do:

Flower.instance_method(:bud).source

=> "  def bud\n    puts 'budding'\n  end\n"

Flower.instance_method(:bud).comment

=> "# opens the flower\n"

This functionality is from a gem called method_source, which is one component of a developer console (alternative to irb) called pry. The method_source gem is now included in the development section of the Gemfile generated by Rails when you create a new application. This makes it easier for developers to quickly access this bit of functionality without having to switch to using pry.

The tests for this gem show all the ins and outs of using it:
https://github.com/banister/method_source/blob/master/test/test.rb

You can read more about the motivations and discussion around this feature on the PR:
https://github.com/rails/rails/issues/18473

This entry was posted in Technology and tagged , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
*

Time limit is exhausted. Please reload the CAPTCHA.