【问题标题】:Rails print (dump) variablesRails 打印(转储)变量
【发布时间】:2014-12-12 13:43:01
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails debugging inspect puts


    【解决方案1】:

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

    p variableputs variable.inspect 的快捷方式。

    【讨论】:

      【解决方案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
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多