2013-01-11 42 views
0

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

dictionary = { 
    "test" => 2, 
    "try" => 2 
} 

puts dictionary 
# => try2test2 

是否有任何其他的方式来做到这一点,以便它会给你的完整列表类似{'test': 2, 'try': 2}

+1

你试过'dictionary.inspect'? – MurifoX

+0

你使用了哪个ruby版本? 'Hash#to_s'不应该像'try2test2'那样返回。 – halfelf

+0

dictionary.inspect works!非常感谢! – tipsywacky

回答

4

由于五Melnychuk提到的,JSON是一个不错的选择,只记得第一个导入 “json的” 模块:

require "json" 
dictionary.to_json 
一般

,您可以通过调用 retreive对象的可读字符串版本就可以了检查:

dictionary.inspect 

最后,有一个“PP”模块,漂亮地打印变量(非常像pprint模块在python):

require "pp" 
pp dictionary 

希望它能帮助!

+0

谢谢,我忘了模块导入 – vmeln

2

尝试对象转换为JSON

dictionary.to_json 
0

你也可以做p dictionary它发送inspect默认:

dictionary = { 
    "test" => 2, 
    "try" => 2 
} 

p dictionary  # => {"test"=>2, "try"=>2}