7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails console (や irb) でデバッガを使って gem とかの挙動を確認する方法

Posted at

rails だったら、まずは Gemfile の gem 'debugger' を有効にする。

$ rails c --debugger
> debugger
(rdb:1) b path/to/code:line
(rdb:1) c
> User.where(:id => 1).first

irb のときは、

$ irb -rubygems -rdebugger
> debugger

要は debugger コンソールでブレークポイントを設定してから c (continue) で抜けてからコードを実行する。
debugger 内でコードを実行してもブレークしない。

例:

$ rails c --debugger
> debugger
(rdb:1) b ~/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:663
Breakpoint 1 file /Users/hiroshi/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb, line 663
(rdb:1) c
> User.where(:id => 1).first
Breakpoint 1 at /Users/hiroshi/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:663
[658, 667] in /Users/hiroshi/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb
   658        def substitute_at(column, index)
   659          Arel::Nodes::BindParam.new "$#{index + 1}"
   660        end
   661  
   662        def exec_query(sql, name = 'SQL', binds = [])
=> 663          log(sql, name, binds) do
   664            result = binds.empty? ? exec_no_cache(sql, binds) :
   665                                    exec_cache(sql, binds)
   666  
   667            ret = ActiveRecord::Result.new(result.fields, result_as_array(result))
/Users/hiroshi/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:663
log(sql, name, binds) do

いつものようにもっとキレイなやり方ありそうなので知ってる人は教えてください。

7
7
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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?