Can't get nested hash value

Okay, a newbie issue. Ruby 1.9 3, and the requires are there for other
stuff. I'm trying to access the values of a particular nested hash, but
seem to missing something critical.

The following code gives the following error.

#### Code

#!/opt/puppet/bin/ruby
#encoding: utf-8

···

#
require 'rubygems'
require 'json'
require 'pp'

id_hash = Hash.new

id_hash["victor"] = Hash.new
id_hash["victor"] = [ "v-123" => "First issue", "v-234" => "Second issue"]
puts "Testing: #{id_hash["victor"]["v-123"]}."

exit

#### Error

12:in `[]': can't convert String into Integer (TypeError)
        from ./make_id_hash.rb:12:in `<main>'

####

What am I missing? Line 12 is "puts" line.

Leam

--
Mind on a Mission <http://leamhall.blogspot.com/>

The problem is in this line. This is an array with 2 hashes in it. You have
to use {..} instead of [...]

ruby-2.1.5 juanjo@ironbird:~$ irb
2.1.5 :001 > [ "v-123" => "First issue", "v-234" => "Second issue"]
=> [{"v-123"=>"First issue", "v-234"=>"Second issue"}]
2.1.5 :002 > {"v-123" => "First issue", "v-234" => "Second issue"}
=> {"v-123"=>"First issue", "v-234"=>"Second issue"}

···

2015-04-08 17:20 GMT-03:00 leam hall <leamhall@gmail.com>:

id_hash["victor"] = [ "v-123" => "First issue", "v-234" => "Second issue"]

--
Juanjo Conti
Mi primer novela ya se puede conseguir en:

Quoting leam hall (leamhall@gmail.com):

   Okay, a newbie issue. Ruby 1.9 3, and the requires are there for other
   stuff. I'm trying to access the values of a particular nested hash, but
   seem to missing something critical.

The fundamental element here is that with the statement

   id_hash["victor"] = Hash.new

a new, empty hash is created and assigned to your variable, but with
the next statement

   id_hash["victor"] = [ "v-123" => "First issue", "v-234" => "Second issue"]

you throw away the hash you just created, and assign to
id_hash["victor"] a new *array* (delimited with square brackets). But
the syntax inside the square brackets is not valid for an array. Thus,
the error.

Either you only keep the line

   id_hash["victor"]={"v-123"=>"First issue","v-234"=>"Second issue"}

(with curly brackets) which creates and populates a hash, or you
create and populate your hash as follows:

   id_hash["victor"]=Hash.new
   id_hash["victor"]["v-123"]="First issue"
   id_hash["victor"]["v-234"]="Second issue"

Carlo

···

Subject: Can't get nested hash value
  Date: mer 08 apr 15 04:20:41 -0400

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

It should be:
id_hash["victor"] = Hash[ "v-123" => "First issue", "v-234" => "Second issue"]
for the old hash syntax. You can also kill the id_hash["victor"] = Hash.new line.

···

     On Wednesday, April 8, 2015 5:39 PM, leam hall <leamhall@gmail.com> wrote:
   
Okay, a newbie issue. Ruby 1.9 3, and the requires are there for other stuff. I'm trying to access the values of a particular nested hash, but seem to missing something critical.

The following code gives the following error.

#### Code

#!/opt/puppet/bin/ruby
#encoding: utf-8
#
require 'rubygems'
require 'json'
require 'pp'

id_hash = Hash.new

id_hash["victor"] = Hash.new
id_hash["victor"] = [ "v-123" => "First issue", "v-234" => "Second issue"]
puts "Testing: #{id_hash["victor"]["v-123"]}."

exit

#### Error

12:in `[]': can't convert String into Integer (TypeError)
from ./make_id_hash.rb:12:in `<main>'

####

What am I missing? Line 12 is "puts" line.

Leam

--
Mind on a Mission

Actually, an array with 1 hash of 2 elements in it.

···

2015-04-08 17:29 GMT-03:00 Juanjo Conti <jjconti@gmail.com>:

2015-04-08 17:20 GMT-03:00 leam hall <leamhall@gmail.com>:

id_hash["victor"] = [ "v-123" => "First issue", "v-234" => "Second issue"]

The problem is in this line. This is an array with 2 hashes in it.

--
Juanjo Conti
Mi primer novela ya se puede conseguir en:

Ah-ha! Thanks!

Leam

···

On 04/09/15 09:17, David R wrote:

It should be:

id_hash["victor"] = Hash[ "v-123" => "First issue", "v-234" => "Second
issue"]

for the old hash syntax. You can also kill the id_hash["victor"] =
Hash.new line.

On Wednesday, April 8, 2015 5:39 PM, leam hall <leamhall@gmail.com> wrote:

Okay, a newbie issue. Ruby 1.9 3, and the requires are there for other
stuff. I'm trying to access the values of a particular nested hash, but
seem to missing something critical.

The following code gives the following error.

#### Code

#!/opt/puppet/bin/ruby
#encoding: utf-8
#
require 'rubygems'
require 'json'
require 'pp'

id_hash = Hash.new

id_hash["victor"] = Hash.new
id_hash["victor"] = [ "v-123" => "First issue", "v-234" => "Second issue"]
puts "Testing: #{id_hash["victor"]["v-123"]}."

exit

#### Error

12:in `': can't convert String into Integer (TypeError)
         from ./make_id_hash.rb:12:in `<main>'

####

What am I missing? Line 12 is "puts" line.

Leam

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;