Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 检查散列_Ruby_Hash_Inspect - Fatal编程技术网

Ruby 检查散列

Ruby 检查散列,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

当我想调试下面的散列时,它返回try2test2


是否有其他方法可以这样做,从而提供完整的列表,如{'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}