【发布时间】:2008-12-09 22:55:24
【问题描述】:
当我运行一个简单的 Ruby 脚本时,将对象的字段转储到控制台的最简单方法是什么?
我正在寻找类似于 PHP 的 print_r() 的东西,它也适用于数组。
【问题讨论】:
标签: ruby printf-debugging
当我运行一个简单的 Ruby 脚本时,将对象的字段转储到控制台的最简单方法是什么?
我正在寻找类似于 PHP 的 print_r() 的东西,它也适用于数组。
【问题讨论】:
标签: ruby printf-debugging
可能:
puts variable.inspect
【讨论】:
inspect 方法允许您定义如何显示类的属性,而不是依赖默认输出。很多类都没有很好地实现它,但是在调试时它确实很有用。如果 Ruby 找不到 inspect` 方法,它将回退到 to_s。
server = TCPServer.new 0 ; puts server.inspect #<TCPServer:fd 9> => nil 。它不适用于大多数复杂的对象。
var_dump 等效项时找到的第一个答案,我发现 pp 在这种情况下非常有用,请看这里 - stackoverflow.com/questions/6501506/ruby-inspect-readability/…
p object 是 alias 的 puts object.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"]
【讨论】:
instance_methods 以获得唯一的方法通常很有用:(String.instance_methods - Object.instance_methods).sort
.methods.sort 非常有用。是否有任何“智能”方法可以快速显示该特定对象(模糊)独特的方法?例如。像.to_s 这样的方法可能会经常出现,所以它并不是那么有用,但有些方法可能非常方便了解某些对象的某些方法。特别是在不明显的情况下。有什么方法可以快速获得这些吗? (例如,我有一个 PG::Result 对象,并且想快速评估我可能会发现有用的可能方法。
to_yaml 方法有时似乎很有用:
$foo = {:name => "Clem", :age => 43}
puts $foo.to_yaml
返回
---
:age: 43
:name: Clem
(这是否取决于正在加载的某些 YAML 模块?或者这通常可用?)
【讨论】:
to_yaml 需要加载 YAML 模型。不过,它是 Ruby 标准库的一部分。
【讨论】:
如果您只查找对象中的实例变量,这可能很有用:
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(":")}
【讨论】:
把 foo.to_json
可能会派上用场,因为默认情况下会加载 json 模块
【讨论】:
to_json 在 1.8.7 或 1.9.2 中默认不加载。
如果你想打印一个已经缩进的 JSON:
require 'json'
...
puts JSON.pretty_generate(JSON.parse(object.to_json))
【讨论】:
我遇到了这个帖子,因为我正在寻找类似的东西。我喜欢这些回复,他们给了我一些想法,所以我测试了 .to_hash 方法,并且在用例中也非常有效。苏:
object.to_hash
【讨论】:
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]
【讨论】:
undefined method 'attributes' for ...
object.attributes_name 不起作用,但 object.attributes 确实可以得到很好的键和值散列。这对我有帮助,谢谢!
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)>
【讨论】:
我正在使用自己的解决方案来打印和调试变量是https://github.com/igorkasyanchuk/wrapped_print
您可以简单地调用user.wp 在日志中查看此变量的值
代替:
puts "-"*10
puts user.inspect
puts "-"*10
【讨论】: