Answers:
可能:
puts variable.inspect
server = TCPServer.new 0 ; puts server.inspect #<TCPServer:fd 9> => nil
。它不适用于大多数复杂的对象。
var_dump
相当于在Ruby中,我发现pp
很多usaful在这种情况下,看看这里- stackoverflow.com/questions/6501506/ruby-inspect-readability/...
您可能会发现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
对象,并且想快速评估我可能发现有用的可能方法
如果要打印已经缩进的JSON:
require 'json'
...
puts JSON.pretty_generate(JSON.parse(object.to_json))
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
确实得到了很好的键和值哈希。这帮助了我,谢谢!
inspect
您的类添加方法可以使您定义如何显示类的属性,而不是依赖默认输出。许多类都无法很好地实现它,但是在调试时它确实很有用。to_s
如果无法找到inspect`方法,Ruby就会退一步。