How to read code without ripping your hair out

Sun-Li Beatteay
Aug 15, 2017 · 7 min read
Image for post
Image for post
Image for post
Image for post
Yes, you.

1. Pick a specific point of interest

Image for post
Image for post
Image Source: https://thenextweb.com/wp-content/blogs.dir/1/files/2010/04/twitter-location-300x200.jpg
Image for post
Image for post

2. Find a loose thread

Image for post
Image for post
Image Source: https://glenelmadventblog.files.wordpress.com/2012/12/loose-thread.jpg
class Comment < ActiveRecord::Base
belongs_to :creator, foreign_key: 'user_id', class_name: 'User'
belongs_to :post
binding.pry
validates :body, presence: true
end
pry> class = belongs_to(:post).class
=> ActiveRecord
::Reflection::AssociationReflection
pry> class.ancestors
=>
[ActiveRecord::Reflection::AssociationReflection,
ActiveRecord::Reflection::MacroReflection,
...omitted for brevity ]
pry> defined? belongs_to
=> "method"
pry> method(:belongs_to).owner
=> ActiveRecord::Associations::ClassMethods

3. Follow the trail

Image for post
Image for post
Image for post
Image for post

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
belongs_to method inside of ClassMethods

Image for post
Image for post
build method in BelongsTo class
Image for post
Image for post
build method in Association class

Wrapping Up

Launch School

Publications of the Launch School Community

Sun-Li Beatteay

Written by

Software Engineer @DigitalOcean. Co-author of Conclave: a decentralized collaborative text editor. https://sunli.co

Launch School

Publications of the Launch School Community

Sun-Li Beatteay

Written by

Software Engineer @DigitalOcean. Co-author of Conclave: a decentralized collaborative text editor. https://sunli.co

Launch School

Publications of the Launch School Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store