2

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

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
Sjaak Rusma
  • 1,424
  • 3
  • 23
  • 36
  • Not an answer as to why this is happening but why dont you try to use symbols as indexes in the hash: `hash = {:a=>["B", "C"], :b=>["C"], :c=>["D", "E"], :d=>["F"]}` and this way `puts hash[:a]` works – Agush Apr 22 '14 at 20:17
  • Hey thanks for your comment. I havent done that because I am setting the keys dynamic – Sjaak Rusma Apr 22 '14 at 20:27
  • Try setting the keys with the `.to_sym` method of String http://apidock.com/ruby/String/to_sym – Agush Apr 22 '14 at 20:29
  • Aha, works great. But look at the answer below. This will also convert the weird 65279 to a symbol.. – Sjaak Rusma Apr 22 '14 at 20:33
  • Yeah there seems to be a problem with your input then, you should sanitize it before converting it to a hash :/ – Agush Apr 22 '14 at 20:35
  • Your `"a"` key has a [BOM](http://en.wikipedia.org/wiki/Byte_order_mark) (byte-order-mark) as the first character. You need to open the file using the appropriate encoding. At a guess I'd say you're working with UTF-16LE. – the Tin Man Apr 22 '14 at 21:39

2 Answers2

7

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
Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • Pfff after hours of debugging this appears to be the case.. I am reading the values out of a .txt file. Not sure how I am going to fix this.. Super thanks anyhow – Sjaak Rusma Apr 22 '14 at 20:32
  • Wow, this is remarkable. If I add one empty line at the beginning of the file everything works. – Sjaak Rusma Apr 22 '14 at 20:35
  • @SjaakRusma or you could try this when reading the file: http://stackoverflow.com/a/7780559/481229 – Agush Apr 22 '14 at 20:37
  • Removed the BOM with an HEX editor. Sad enough, doing things like File.open('foo', 'rb:BOM|UTF-16LE') do |fi| did not work. – Sjaak Rusma Apr 24 '14 at 14:56
2

Your "a" key has a BOM (byte-order-mark) as the first character. You need to open the file using the appropriate encoding. At a guess I'd say you're working with UTF-16LE.

File.open('foo', 'rb:BOM|UTF-16LE') do |fi|
  ...
end

See http://www.ruby-doc.org/core-2.1.1/IO.html#method-c-new-label-IO+Encoding for more information.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303