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

当我运行一个简单的Ruby脚本时,将对象的字段转储到控制台最简单的方法是什么?

我正在寻找类似于PHP的print_r() ,它也可以用于数组。

    可能是:

     puts variable.inspect 

    你可能会发现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"] 

    to_yaml方法有时似乎是有用的:

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

    回报

     --- :age: 43 :name: Clem 

    (这是否取决于正在加载的一些YAML模块?或者通常可用?

     p object 

    Ruby文档为p

    p(*args) public

    对于每个对象,直接写obj.inspect,后面跟着换行符到程序的标准输出。

    如果你只是在对象中寻找实例variables,这可能是有用的:

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

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

     obj.instance_variables.map{|var| puts [var, obj.instance_variable_get(var)].join(":")} 

    放foo.to_json

    可能会派上用场,因为json模块是默认加载的

    如果你想打印一个已经缩进的JSON

     require 'json' ... puts JSON.pretty_generate(JSON.parse(object.to_json)) 
     object.attributes_name 

    >“”id“”name“”email“”created_at“updated_at”password_digest“”remember_token“”admin“”marketing_permissions“”terms_and_conditions“”禁用“”black_list“ ,“zero_cost”,“password_reset_token”,“password_reset_sent_at”]

     object.attributes.values 

    => [1,“tom”,“tom@tom.com”,2015年6月2日星期二00:16:03 UTC +00:00,星期二,2015年6月02日00:22:35 UTC +00:00, $ 2a $ 10 $ gUTr3lpHzXvCDhVvizo8Gu / MxiTrazOWmOQqJXMW8gFLvwDftF9Lm“,”2dd1829c9fb3af2a36a970acda0efe5c1d471199“,true,nil,nil,nil,nil,nil,nil,nil]

    我遇到这个线程,因为我正在寻找类似的东西。 我喜欢这些反应,他们给了我一些想法,所以我testing了.to_hash方法,并且也非常适合用例。 洙:

    object.to_hash

    希望这个答案可以帮助别人!