LoginSignup
15
15

More than 5 years have passed since last update.

pryでメソッドの挙動を最初からトレースする。

Posted at

概要

binding.pry; Hoge.hogeでトレースを開始することができます。
編集してbinding.pryを埋め込む必要がないので便利です。

使用例

RailsのModelを新規作成するときの挙動が知りたい!

[28] pry(main)> binding.pry; User.new name: "sachaos"

Frame number: 0/20

From: /Users/hogehoge/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.2/lib/active_record/inheritance.rb @ line 49 ActiveRecord::Inheritance::ClassMethods#new:

    48: def new(*args, &block)
 => 49:   if abstract_class? || self == Base
    50:     raise NotImplementedError, "#{self} is an abstract class and cannot be instantiated."
    51:   end
    52:
    53:   attrs = args.first
    54:   if subclass_from_attributes?(attrs)
    55:     subclass = subclass_from_attributes(attrs)
    56:   end
    57:
    58:   if subclass
    59:     subclass.new(*args, &block)
    60:   else
    61:     super
    62:   end
    63: end

[1] pry(User)> args
=> [{:name=>"sachaos"}]

捗りますね。

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15