diff --git a/README.md b/README.md index 36bab36..c0efa70 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Are They All True?](ruby/are-they-all-true.md) - [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md) - [Destructuring Arrays In Blocks](ruby/destructuring-arrays-in-blocks.md) +- [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md) - [Limit Split](ruby/limit-split.md) - [Parallel Bundle Install](ruby/parallel-bundle-install.md) - [Summing Collections](ruby/summing-collections.md) diff --git a/ruby/finding-the-source-of-ruby-methods.md b/ruby/finding-the-source-of-ruby-methods.md new file mode 100644 index 0000000..ed1e75f --- /dev/null +++ b/ruby/finding-the-source-of-ruby-methods.md @@ -0,0 +1,29 @@ +# Finding The Source of Ruby Methods + +Ruby's [`Method`](http://ruby-doc.org/core-1.9.3/Method.html) class +includes a feature that can help you quickly find the location of +source code files where a particular method is defined. The method is aptly +named [`source_location`](http://ruby-doc.org/core-1.9.3/Method.html#method-i-source_location). + +When debugging a project that is using the +[Treat](https://github.com/louismullie/treat) gem, you can take a source dive +by first finding the relevant source files. For instance, if you want to look +into the word creation functionality, you might go through an exploratory +process like the following: + +```ruby +> require 'Treat' +=> true +> Treat::Entities::Word.build('stuff') +=> Word (70331843958460) --- "stuff" --- {} --- [] +> Treat::Entities::Word.method(:build) +=> # +> Treat::Entities::Word.method(:build).source_location +=> ["/Users/jbranchaud/.gem/ruby/2.1.4/gems/treat-2.1.0/lib/treat/entities/entity/buildable.rb", 29] +``` + +You can now take a closer look at the implementation of the `build` method. + +The main caveat to this process is that it can only find source locations +for methods defined in ruby land. Calling `source_location` on any method +defined in C code will result in `nil`.