当前位置:  开发笔记 > 编程语言 > 正文

在Ruby/Rails中PHP的print_r是否相同?

如何解决《在Ruby/Rails中PHP的print_r是否相同?》经验,为你挑选了5个好方法。

在PHP中,您可以:

print_r($var) 要么 vardump($var)

它打印有关变量的"人类可读"信息.

Ruby/Rails中是否有等效的函数/帮助器?



1> Honza..:

在Rails模板中,您可以执行此操作

<%= debug an_object %>

它会做很好的HTML PRE输出.



2> Antibaddy..:

尝试使用pp.您需要在脚本中使用它(如果您的.irbc尚未执行此操作,则需要在irb中):

require 'pp'

那么你可以'PrettyPrint'这样一个对象:

pp object



3> Chirantan..:

你可以简单地做,而不是要求'pp'和使用pp

p object

经过测试的例子

require 'pp'

class A
  def initialize
    @a = 'somevar'
    @b = [1,2,3]
    @c = {'var' => 'val'}
  end
end

a = A.new
pp a # Gives -> #"val"}>
p a # Gives -> #"val"}>. No need to require 'pp'



4> dylanfm..:

inspect帮助的方法.有时to_s在对象上调用方法会有所帮助(to_s返回对象的字符串表示).您还可以查询methods,local_variables,class_variables,instance_variables,constantsglobal_variables.

p ['Hello',"G'day",'Bonjour','Hola'].inspect
# >> "[\"Hello\", \"G'day\", \"Bonjour\", \"Hola\"]"

p ['Hello',"G'day",'Bonjour','Hola'].to_s
# >> "HelloG'dayBonjourHola"

p Array.new.methods
# >> ["select", "[]=", "inspect", "compact"...]

monkey = 'baboon'
p local_variables
# >> ["monkey"]

class Something
  def initialize
    @x, @y = 'foo', 'bar'
    @@class_variable = 'gorilla'
  end
end

p Something.class_variables
# >> ["@@class_variable"]

s = Something.new
p s.instance_variables
# >> ["@x", "@y"]

p IO.constants
# >> ["TRUNC", "SEEK_END", "LOCK_SH"...]

p global_variables
# >> ["$-d", "$\"", "$$", "$<", "$_", "$-K"...]



5> Bill_B..:

我知道这是一个老帖子,但这是Google在搜索"Ruby等效的PHP print_r"时弹出的第一件事.我在命令行模式下使用Ruby,并且确实没有非常好的等价物."pp"对于相当简单的结构是可以的,但是一旦你开始在更多数组中的哈希中的数组中嵌套哈希,它就变得非常快.由于我没有找到print_r的良好模拟,我自己写了一个.这对我的目的来说已经足够了,不会过于复杂,我想我会分享它以挽救其他人一些头疼的问题.将输出与真正的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

这是一个例子(故意弄乱,以显示为什么PHP print_r非常好):

    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)

print_r的输出(实际上是人类可以理解的):

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

推荐阅读
低调pasta_730
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有