どのように私はコンソールにオブジェクトのフィールドをダンプするのですか?

StackOverflow https://stackoverflow.com/questions/354547

  •  21-08-2019
  •  | 
  •  

質問

私は、単純なRubyスクリプトを実行している場合は、

、コンソールにオブジェクトのフィールドをダンプする最も簡単な方法は何ですか?

私も配列で動作する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モジュールに依存していますか?それともそれは一般的に利用できるようになる?)

p object

pのためのRubyのドキュメント。

  

p(*args) publicする

     

各オブジェクトについて、直接続くobj.inspect書き込みます。   プログラムの標準出力に改行でます。

あなたがオブジェクト内だけでインスタンス変数を探しているなら、これは役に立つかもしれません。

obj.instance_variables.map do |var|
  puts [var, obj.instance_variable_get(var)].join(":")
end

またはコピーと貼り付けのための1つのライナーとして

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))
私は似たような探していたので、

私はこのスレッドに出くわしました。私は応答が好きで、私は.to_hash方法をテストし、あまりにもユースケースのために本当によく働いていたので、彼らは私にいくつかのアイデアを与えました。秀ます:

object.to_hash

object.attributes_name

=> [ "ID"、 "名"、 "電子メール"、 "のcreated_at"、 "updated_atの"、 "password_digest"、 "remember_token"、 "管理者"、 "marketing_permissions"、 "terms_and_conditions"、 "無効"、 "black_list"、 "zero_cost"、 "password_reset_token"、 "password_reset_sent_at"

object.attributes.values

=> [1、 "トム"、 "tom@tom.com"、火、2015年6月2日0時16分03秒UTC +00:00、火、2015年6月2日0時22分35秒UTC +00: 00、 "$ 2A $ 10 $ gUTr3lpHzXvCDhVvizo8Gu / MxiTrazOWmOQqJXMW8gFLvwDftF9Lm"、 "2dd1829c9fb3af2a36a970acda0efe5c1d471199"、真、ゼロ、ゼロ、ゼロ、ゼロ、ゼロ、ゼロ、ゼロ]

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top