Ruby 检查散列
当我想调试下面的散列时,它返回try2test2Ruby 检查散列,ruby,hash,inspect,Ruby,Hash,Inspect,当我想调试下面的散列时,它返回try2test2 是否有其他方法可以这样做,从而提供完整的列表,如{'test':2'try':2}?尝试将对象转换为JSON dictionary.to_json 正如V.Melnychuk所提到的,JSON是一个不错的选择,请记住首先导入JSON模块: require "json" dictionary.to_json 通常,您可以通过调用 检查它: dictionary.inspect 最后,还有一个pp模块,用于打印变量,非常类似于python中的p
是否有其他方法可以这样做,从而提供完整的列表,如{'test':2'try':2}?尝试将对象转换为JSON
dictionary.to_json
正如V.Melnychuk所提到的,JSON是一个不错的选择,请记住首先导入JSON模块:
require "json"
dictionary.to_json
通常,您可以通过调用
检查它:
dictionary.inspect
最后,还有一个pp模块,用于打印变量,非常类似于python中的pprint模块:
require "pp"
pp dictionary
希望有帮助 您也可以使用默认情况下发送inspect的p字典:
dictionary = {
"test" => 2,
"try" => 2
}
p dictionary # => {"test"=>2, "try"=>2}
你试过dictionary.inspect吗?你用的是哪个ruby版本?Hashto_不应该像try2test2.dictionary.inspect那样返回!非常感谢!谢谢,我忘了模块导入
dictionary = {
"test" => 2,
"try" => 2
}
p dictionary # => {"test"=>2, "try"=>2}