문제
면 나는 실행하는 간단하는 루비 스크립트,무엇이 가장 쉬운 방법을 덤프하는 개체의 필드를 콘솔?
내가 찾는 비슷한 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
모듈이로드 중입니까? 아니면 일반적으로 사용할 수 있습니까?)
객체의 인스턴스 변수 만 찾고 있다면 유용 할 수 있습니다.
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.to_hash
object.attributes_name
=> [ "id", "name", "email", "create_at", "updated_at", "password_digest", "remember_token", "admin", "marketing_permissions", "orver_and_conditions", "disable", "black_list" , "Zero_cost", "password_reset_token", "password_reset_sent_at"]]
object.attributes.values
=> [1, "tom", "tom@tom.com", tue, 2015 년 6 월 2 일 00:16:03 UTC +00 : 00, tue, 02 2015 년 6 월 2 일 00:22:35 UTC +00 : 00, " $ 10 $ 10 $ 10 GUTR3LPHZXVCDHVVIZO8GU/MXITRAZOWMOQQJXMW8GFLVWDFTF9LM ","2DD1829C9FB3AF2A36A970ACDA0EFE5C1D471199 ", TRUE, NIL, NIL, NIL, NIL, NIL, NIL