为什么我不能从散列中读取值? [英] Why can't I read a value from a hash?

查看:154
本文介绍了为什么我不能从散列中读取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

  hash = {a=> [B,C] ,b=> [C],c=> [D,E],d=> [F]} 
puts hash [a ]

这只是不打印任何东西。

  puts hash [a]。class 

打印 NilClass



是否有某种已知的错误在下面的ruby版本中?


ruby​​ 2.0.0p247(2013-06-27 revision 41674)[universal.x86_64-darwin13] p>

我希望有人能帮助我,这让我疯狂。我的IDE是JetBrains的RubyMine。我也尝试通过IRB直接运行它。

谢谢

PS。 OS是OSX

解决方案

通过复制代码并将其粘贴到我的控制台中,我可以重现您的问题。 b
$ b

然后我把你的散列做了以下操作:

  hash.first.first 
#=> a
hash.first.first.length
#=> 2

!!!



看来你的a有一个不可打印的第一个字符。删除它,你会没事的。



你的字符是这是什么字符? 65279''

  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?

ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]

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

这篇关于为什么我不能从散列中读取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆