How do I dump an object's fields to the console?

后端 未结 9 2264
谎友^
谎友^ 2020-12-12 10:09

When I\'m running a simple Ruby script, what\'s the easiest way to dump an object\'s fields to the console?

I\'m looking for something similar to PHP\'s print

9条回答
  •  温柔的废话
    2020-12-12 10:52

    The to_yaml method seems to be useful sometimes:

    $foo = {:name => "Clem", :age => 43}
    
    puts $foo.to_yaml
    

    returns

    --- 
    :age: 43
    :name: Clem
    

    (Does this depend on some YAML module being loaded? Or would that typically be available?)

提交回复
热议问题