当我运行一个简单的Ruby脚本时,将对象的字段转储到控制台的最简单方法是什么?
我正在寻找与PHP的print_r()类似的东西,它也将与数组一起工作。
当我运行一个简单的Ruby脚本时,将对象的字段转储到控制台的最简单方法是什么?
我正在寻找与PHP的print_r()类似的东西,它也将与数组一起工作。
当前回答
您可能会发现method方法的用法,它为对象返回一个方法数组。它与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"]
其他回答
可能是:
puts variable.inspect
如果你只是在对象中寻找实例变量,这可能是有用的:
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(":")}
我使用自己的解决方案打印和调试变量是https://github.com/igorkasyanchuk/wrapped_print
您可以简单地调用user。Wp在日志中查看该变量的值
而不是:
puts "-"*10
puts user.inspect
puts "-"*10
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)>
您可能会发现method方法的用法,它为对象返回一个方法数组。它与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"]