We can use pry and pry-byebug gem to help us dive into the source code.
Let’s see an example about to_json vs as_json
I found there’s a to_json and as_json method for active record query result.
to_json returns String of json.
as_json returns a hash representing the model.
| 1 2 3 4 5 6 7 8 9 |  | 
I find that as_json can accepts some optons.
| 1 2 |  | 
But one day I found somebody used similar options with to_json method.
I searched the doc. No doc says to_json can do things like following:
| 1 2 |  | 
But the code above actually works.
Is to_json uses as_json under the hood?
Dive into the source
Now we can check the source code to find the answer.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |  | 
Then we edit the method and add a binding.pry before the if options.is_a?(::JSON::State) line.
Remember to recover the change after debug.
> edit Order.where(number: "201504211405490").to_json
Then run code again:
> Order.where(number: "201504211405490").to_json
Now we’ll get into to_json_with_active_support_encoder method and start to debug.
After some next and step comand.
We can find that to_json uses ActiveSupport::JSON.encode, which uses as_json.
# /lib/active_support/json/encoding.rb#34
# Encode the given object into a JSON string
def encode(value)
  stringify jsonify value.as_json(options.dup)
end
Yes! to_json uses as_json. We find the answer. So we can use to_json(only: :number) with
confidence.
Conclusion
Pry and pry-byebug provides many useful commands. Using these commands can help to find the definition of a method, track the call stacks, understand the source structure.
This is beauty of open source. Do not limit yourself. Dive into the code and get your information.