问题描述
考虑以下代码: hash = {a=> [B,C] ,b=> [C],c=> [D,E],d=> [F]}
puts hash [a ]
这只是不打印任何东西。
puts hash [a]。class
打印 NilClass
是否有某种已知的错误在下面的ruby版本中?
我希望有人能帮助我,这让我疯狂。我的IDE是JetBrains的RubyMine。我也尝试通过IRB直接运行它。
谢谢
PS。 OS是OSX
通过复制代码并将其粘贴到我的控制台中,我可以重现您的问题。 b
$ b
然后我把你的散列做了以下操作:
hash.first.first
#=> a
hash.first.first.length
#=> 2
!!!
看来你的a
有一个不可打印的第一个字符。删除它,你会没事的。
你的字符是:
hash.first.first [0] .ord
#=> 65279
Considering the following code:
hash = {"a"=>["B", "C"], "b"=>["C"], "c"=>["D", "E"], "d"=>["F"]}
puts hash["a"]
This just prints nothing.
puts hash["a"].class
This prints NilClass
Is there some kind of known bug in the following ruby version?
I hope someone can help me out, this is driving me crazy. My IDE is RubyMine from JetBrains. I also tried to run it directly via IRB.
Thanks
PS. OS is OSX
By copying your code and pasting it in my console, I could reproduce your problem.
Then I took your hash and did the following:
hash.first.first
# => "a"
hash.first.first.length
# => 2
!!!
It seems that your "a"
has an unprintable first char. Delete it, and you'll be fine.
And you char is What is this char? 65279 '':
hash.first.first[0].ord
# => 65279
这篇关于为什么我不能从散列中读取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!