Rails打印(转储)变量

3

在调试过程中,我通常使用puts some_variable.inspect来打印(dump)变量。然而,我厌倦了每次都要输入puts ...inspect。是否有更好的方法来打印变量?

提示:代码中的...表示需要填入具体的变量名或表达式。
2个回答

3

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

请查看http://confreaks.com/videos/2864-rubyconf2013-repl-driven-development-with-pry以获取有关如何使用pry进行调试的更多详细信息。 - Amit Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接