Rails打印(转储)变量

时间:2014-10-17 02:50:54

标签: ruby-on-rails debugging inspect puts

调试时,我通常使用puts some_variable.inspect来打印(转储)变量。但是,我厌倦了每次都输入puts ...inspect。有没有更好的方法来打印变量?

2 个答案:

答案 0 :(得分:2)

使用pry调试程序

 #Gemfile
  gem 'pry-rails'

  @data = {content: 'important'}
  #instead of puts @data.inspec
  binding.pry

运行它将在行上保存的代码,其中调用binding.pry并启动pry终端会话。

  pry(main)> @data
  => {:content=>"important"}
   pry(main)> cd @data
   pry(#<Hash>):1> ls -m
  Enumerable#methods:
    all?            count       each_cons         entries     flat_map  map     minmax     reduce        take
    any?            cycle       each_entry        find        grep      max     minmax_by  reverse_each  take_while
    chunk           detect      each_slice        find_all    group_by  max_by  none?      slice_before  zip
    collect         drop        each_with_index   find_index  inject    min     one?       sort
    collect_concat  drop_while  each_with_object  first       lazy      min_by  partition  sort_by

答案 1 :(得分:2)

我发现了这个:http://www.ruby-doc.org/core-2.1.3/Kernel.html#method-i-p

p variableputs variable.inspect的快捷方式。