代码之家  ›  专栏  ›  技术社区  ›  roryf

如何将对象的字段转储到控制台?

  •  232
  • roryf  · 技术社区  · 17 年前

    我正在寻找类似PHP的东西 print_r() 这也适用于阵列。

    9 回复  |  直到 14 年前
        1
  •  455
  •   Christian Lescuyer    17 年前

    可能:

    puts variable.inspect
    
        2
  •  57
  •   dylanfm    17 年前

    你可能会发现它的用处 methods 方法,该方法返回对象的方法数组。这和我的不一样 print_r

    >> "Hello".methods.sort
    => ["%", "*", "+", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", "[]", "[]=", "__id__", "__send__", "all?", "any?", "between?", "capitalize", "capitalize!", "casecmp", "center", "chomp", "chomp!", "chop", "chop!", "class", "clone", "collect", "concat", "count", "crypt", "delete", "delete!", "detect", "display", "downcase", "downcase!", "dump", "dup", "each", "each_byte", "each_line", "each_with_index", "empty?", "entries", "eql?", "equal?", "extend", "find", "find_all", "freeze", "frozen?", "grep", "gsub", "gsub!", "hash", "hex", "id", "include?", "index", "inject", "insert", "inspect", "instance_eval", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "intern", "is_a?", "is_binary_data?", "is_complex_yaml?", "kind_of?", "length", "ljust", "lstrip", "lstrip!", "map", "match", "max", "member?", "method", "methods", "min", "next", "next!", "nil?", "object_id", "oct", "partition", "private_methods", "protected_methods", "public_methods", "reject", "replace", "respond_to?", "reverse", "reverse!", "rindex", "rjust", "rstrip", "rstrip!", "scan", "select", "send", "singleton_methods", "size", "slice", "slice!", "sort", "sort_by", "split", "squeeze", "squeeze!", "strip", "strip!", "sub", "sub!", "succ", "succ!", "sum", "swapcase", "swapcase!", "taguri", "taguri=", "taint", "tainted?", "to_a", "to_f", "to_i", "to_s", "to_str", "to_sym", "to_yaml", "to_yaml_properties", "to_yaml_style", "tr", "tr!", "tr_s", "tr_s!", "type", "unpack", "untaint", "upcase", "upcase!", "upto", "zip"]
    
        3
  •  52
  •   mjs    16 年前

    这个 to_yaml

    $foo = {:name => "Clem", :age => 43}
    
    puts $foo.to_yaml
    

    返回

    --- 
    :age: 43
    :name: Clem
    

    (这是否取决于某些因素 YAML

        4
  •  33
  •   Cees Timmerman    11 年前
    p object
    

    Ruby doc for p .

    p(*args) public

    对于每个对象,直接写入obj.inspect

        5
  •  16
  •   Christopher Oezbek    5 年前

    如果只在对象中查找实例变量,这可能很有用:

    obj.instance_variables.each do |var|
      puts [var, obj.instance_variable_get(var).inspect].join(":")
    end
    

    或作为复制和粘贴的单行线:

    obj.instance_variables.each{ |var| puts [var, obj.instance_variable_get(var).inspect].join(":")}
    
        6
  •  10
  •   tjerk    15 年前

    将foo.to_json

    由于默认情况下加载了json模块,因此可能会派上用场

        7
  •  5
  •   ROMANIA_engineer Alexey    9 年前

    如果您想打印一个 缩进JSON

    require 'json'
    ...
    puts JSON.pretty_generate(JSON.parse(object.to_json))
    
        8
  •  5
  •   Billal Begueradj    8 年前

    object.to_hash

        9
  •  3
  •   stevendaniels Conor    6 年前
    object.attribute_names
    
    # => ["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_token", "admin", "marketing_permissions", "terms_and_conditions", "disable", "black_list", "zero_cost", "password_reset_token", "password_reset_sent_at"]
    
    
    object.attributes.values
    
    # => [1, "tom", "tom@tom.com", Tue, 02 Jun 2015 00:16:03 UTC +00:00, Tue, 02 Jun 2015 00:22:35 UTC +00:00, "$2a$10$gUTr3lpHzXvCDhVvizo8Gu/MxiTrazOWmOQqJXMW8gFLvwDftF9Lm", "2dd1829c9fb3af2a36a970acda0efe5c1d471199", true, nil, nil, nil, nil, nil, nil, nil] 
    
        10
  •  0
  •   QETHAN    5 年前

    pp File.stat('/tmp')

    #<File::Stat
     dev=0x1000004,
     ino=71426291,
     mode=041777 (directory rwxrwxrwt),
     nlink=15,
     uid=0 (root),
     gid=0 (wheel),
     rdev=0x0 (0, 0),
     size=480,
     blksize=4096,
     blocks=0,
     atime=2021-04-20 17:50:33.062419819 +0800 (1618912233),
     mtime=2021-04-21 11:35:32.808546288 +0800 (1618976132),
     ctime=2021-04-21 11:35:32.808546288 +0800 (1618976132)>
    
        11
  •  0
  •   Igor Kasyanchuk    5 年前

    我正在使用自己的解决方案来打印和调试变量 https://github.com/igorkasyanchuk/wrapped_print

    你只要打个电话就可以了 user.wp

    而不是:

    puts "-"*10
    puts user.inspect
    puts "-"*10
    
    推荐文章