# How to Traverse Foreign Ruby Code

DevFeed: [How to Traverse Foreign Ruby Code](<https://devfeed.tech/articles/how-to-traverse-foreign-ruby-code-21047.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/10/25/how-to-traverse-foreign-ruby-code/>)

Published: 2015-10-25T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [Code](<https://devfeed.tech/topics/code.md>), [debug](<https://devfeed.tech/topics/debug.md>), [Sublime Text](<https://devfeed.tech/topics/sublime-text.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [code](<https://devfeed.tech/tags/code.md>), [data](<https://devfeed.tech/tags/data.md>), [debug](<https://devfeed.tech/tags/debug.md>), [debugger](<https://devfeed.tech/tags/debugger.md>), [documentation](<https://devfeed.tech/tags/documentation.md>), [files](<https://devfeed.tech/tags/files.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [json](<https://devfeed.tech/tags/json.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>), [software](<https://devfeed.tech/tags/software.md>), [source](<https://devfeed.tech/tags/source.md>), [third-party](<https://devfeed.tech/tags/third-party.md>), [tools](<https://devfeed.tech/tags/tools.md>)

## AI overview

A tutorial on investigating unfamiliar Ruby code in a Ruby on Rails project. It recommends using a text editor, the pry runtime debugger, Ruby's source and source_location methods, a console, and direct inspection of gem source. The example traces ActiveRecord's store_accessor method and adds a breakpoint to observe its metaprogrammed behavior.

## Source excerpt

A Ruby on Rails project will most likely contain large amounts of third party software. Software written by other people can fluctuate greatly in terms of documentation. Even very well documented software might have pieces that are more shrouded than others. When these opaque pieces of code start to cause issue or incite curiosity, spelunking through these libraries is easier with simple patterns and the right tools. 0. Get the Right Tools An important piece of technology when reading code is a text editor. The right editor will make searching for methods fast and opening files painless. Another vital component when traversing Ruby code is a runtime debugger. For this, the pry gem is a personal favorite. Using pry is as simple as requiring it, then adding binding.pry at the desired stopping point. Alternatively, some people like to debug with output statements in code execution; but, this is 2015 and I like things that I can interact with. 1. Use source and source_location This example will use ActiveRecord's store_accessor method as the subject of investigation. The store_accessor method is useful for storing data with a volatile structure. If an application wants to prototype a feature or is unsure about how useful a certain data structure might be, using store_accessor is reasonable. In this case we can assume that the store_accessor column is the json type. Given a User class, with a column named settings, we can define two store_accessors: is_registered and contact_method. class User < ActiveRecord::Base store_accessor :settings, :is_registered, :contact_method end This model responds to contact_method= and serializes the result into the json column. If we wanted to see how this was defined, we need to start a console and look at where the contact_method= method exists. To see the definition of a method, method and source are helpful. user = User.new puts user.method(:contact_method=).source #=> define_method("#{key}=") do |value| # write_store_attribute(store_