Is there equivalent for PHP's print_r in Ruby / Rails?

前端 未结 8 751
鱼传尺愫
鱼传尺愫 2020-12-08 03:17

In PHP you can do:

print_r($var) or vardump($var)

which prints \"human-readible\" information about variable.

Is there equ

相关标签:
8条回答
  • 2020-12-08 03:22

    One approach I lean on a lot is this:

    logger.debug "OBJECT: #{an_object.to_yaml}"
    

    Easy to read, although it can get a little unwieldy for large objects.

    0 讨论(0)
  • 2020-12-08 03:25

    In Rails templates you can do

    <%= debug an_object %>
    

    and it will do nice HTML PRE output.

    0 讨论(0)
  • 2020-12-08 03:25

    Instead of requiring 'pp' and using pp, you can simply do

    p object
    

    Tested example

    require 'pp'
    
    class A
      def initialize
        @a = 'somevar'
        @b = [1,2,3]
        @c = {'var' => 'val'}
      end
    end
    
    a = A.new
    pp a # Gives -> #<A:0x2c6d048 @a="somevar", @b=[1, 2, 3], @c={"var"=>"val"}>
    p a # Gives -> #<A:0x2c6d048 @a="somevar", @b=[1, 2, 3], @c={"var"=>"val"}>. No need to require 'pp'
    
    0 讨论(0)
  • 2020-12-08 03:28

    Try using pp. You will need to require it in scripts (or in irb if your .irbc doesn't already do this):

    require 'pp'
    

    Then you can 'PrettyPrint' an object thus:

    pp object
    
    0 讨论(0)
  • 2020-12-08 03:29

    Check out the guide for debugging rails: http://guides.rubyonrails.com/debugging_rails_applications.html

    hints: script/console is great to try stuff in the context of your app script/server --debugger to start the server with a debugger turned on, you can then use 'debug' in your code to break into an interactive shell

    0 讨论(0)
  • 2020-12-08 03:36

    I know this is an old post, but it is the first thing that Google pops up when searching for "Ruby equivalent of PHP print_r". I'm using Ruby in the command line mode, and there's really not a very good equivalent. "pp" is ok for fairly simple structures, but as soon as you start nesting hashes in arrays in hashes in more arrays, it turns into a jumble pretty fast. Since I haven't found a good emulation of print_r, I wrote one myself. It's good enough for my purposes, not overly complicated and I thought I'd share it to save other people some headache. Compare the output with the real PHP print_r

    def print_r(inHash, *indent)
        @indent = indent.join
        if (inHash.class.to_s == "Hash") then
            print "Hash\n#{@indent}(\n"
            inHash.each { |key, value|
                if (value.class.to_s =~ /Hash/) || (value.class.to_s =~ /Array/) then
                    print "#{@indent}    [#{key}] => "
                    self.print_r(value, "#{@indent}        ")
                else
                    puts "#{@indent}    [#{key}] => #{value}"
                end
            }
            puts "#{@indent})\n"
        elsif (inHash.class.to_s == "Array") then
            print "Array\n#{@indent}(\n"
            inHash.each_with_index { |value,index|
                if (value.class.to_s == "Hash") || (value.class.to_s == "Array") then
                    print "#{@indent}    [#{index}] => "
                    self.print_r(value, "#{@indent}        ")
                else
                    puts "#{@indent}    [#{index}] => #{value}"
                end
            }
            puts "#{@indent})\n"
        end
        #   Pop last indent off
        8.times {@indent.chop!}
    end
    

    Here's an example (made messy on purpose to show why the PHP print_r is so nice):

        carTools =  [ "Socket Set", "Combination Wrenches", "Oil Filter puller", "Brake Compressor" ]
        houseTools =[ "Circular Saw", "Miter Saw", "Drill" ]
        garageItems = Hash["Car1" => "Ford Mustang", "Car2" => "Honda Civic", "Bike1" => "IronHorse"]
        garageItems["Tools"] = Hash["Car Tools" => carTools, "House Tools" => houseTools]
        constructionSupplies = Hash["Plywood" => ["3/4\" T&G Plywood Sheets", "1/2\" Plywood Sheets"],
                                    "Boards" => ["2x4s", "2x6s", "Engineered I-Joists"],
                                    "Drywall" => ["4x8 1/2\" Sheetrock", "Mesh tape", "Paper tape", "Joint compount"]]
        carParts = Hash["Mustang" => ["Clutch", "Transmission", "3.55 Ring & Pinion Gears", "Differential", "30# Injectors", "Pro-M 77mm MAF"]]
        garageItems["Supplies"] = ["Oil", "WD40", constructionSupplies, carParts, "Brake Fluid"]
        print_r(garageItems)
    

    Output of print_r (actually comprehensible by a human):

        Hash
        (
            [Car1] => Ford Mustang
            [Car2] => Honda Civic
            [Bike1] => IronHorse
            [Tools] => Hash
                (
                    [Car Tools] => Array
                        (
                            [0] => Socket Set
                            [1] => Combination Wrenches
                            [2] => Oil Filter puller
                            [3] => Brake Compressor
                        )
                    [House Tools] => Array
                        (
                            [0] => Circular Saw
                            [1] => Miter Saw
                            [2] => Drill
                        )
                )
            [Supplies] => Array
                (
                    [0] => Oil
                    [1] => WD40
                    [2] => Hash
                        (
                            [Plywood] => Array
                                (
                                    [0] => 3/4" T&G Plywood Sheets
                                    [1] => 1/2" Plywood Sheets
                                )
                            [Boards] => Array
                                (
                                    [0] => 2x4s
                                    [1] => 2x6s
                                    [2] => Engineered I-Joists
                                )
                            [Drywall] => Array
                                (
                                    [0] => 4x8 1/2" Sheetrock
                                    [1] => Mesh tape
                                    [2] => Paper tape
                                    [3] => Joint compount
                                )
                        )
                    [3] => Hash
                        (
                            [Mustang] => Array
                                (
                                    [0] => Clutch
                                    [1] => Transmission
                                    [2] => 3.55 Ring & Pinion Gears
                                    [3] => Differential
                                    [4] => 30# Injectors
                                    [5] => Pro-M 77mm MAF
                                )
                        )
                    [4] => Brake Fluid
                )
        )
    
    0 讨论(0)
提交回复
热议问题