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

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

如何解决《如何将对象的字段转储到控制台?》经验,为你挑选了7个好方法。

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

我正在寻找类似于PHP的东西print_r(),它也适用于数组.



1> Christian Le..:

可能是:

puts variable.inspect


在类中添加[`inspect`](http://www.ruby-doc.org/ruby-1.9/classes/Object.html#M000225)方法可以定义类的属性显示方式,而不是依赖于默认输出.很多类都没有很好地实现它,但在调试时它确实非常有用.如果找不到inspect`方法,Ruby将回退到`to_s`.
`server = TCPServer.new 0; put server.inspect# => nil`.它不适用于大多数复杂的对象.
当前链接已损坏,请参阅此内容http://ruby-doc.org/core-2.0/Object.html#method-i-inspect

2> dylanfm..:

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


使用内省是Ruby乐趣的一部分.从有问题的类中减去Object的`instance_methods`来获取独特的方法通常很有用:`(String.instance_methods - Object.instance_methods).sort`
这应该是正确的答案,因为我在找到这个页面时期待这个.

3> mjs..:

to_yaml方法有时似乎很有用:

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

puts $foo.to_yaml

回报

--- 
:age: 43
:name: Clem

(这取决于YAML正在加载的某个模块吗?或者这通常是否可用?)


是的,`to_yaml`需要加载YAML模型.但它是Ruby标准库的一部分.

4> rampion..:
p object

Ruby doc for p.

p(*args) public

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


nope,`p object`表示`puts object.inspect`

5> Mike..:

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

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(":")}



6> 小智..:

把foo.to_json

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


默认情况下,不会在1.8.7或1.9.2中加载`to_json`.

7> ROMANIA_engi..:

如果要打印已缩进的JSON:

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

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