Add value inside 'Nested Mappings String'


Im trying to add some data/value into my nested mapping file(strings)
output but its not working. Below the example of my output:

{“name”=>“Health & Safety”,
[{“name”=>“Training and Culture”,

For example how can i change the children name from “Training and
Culture” to let say “Objective”? Do i need to build a new hash for this
data and modify that hash? Thanks in advance.


Perhaps you’re not considering that “children” returns an Array!
The Hash inside it is the 0 indexed element of this Array.

a = {“System”=>
{“name”=>“Health & Safety”,
[{“name”=>“Training and Culture”,


now ‘a’ returns

{“System”=>{“H&S”=>{“name”=>“Health & Safety”,
“components”=>[“FlashWheel”], “children”=>[{“name”=>“Objective”,
“components”=>“None”, “subtype”=>“None”, “type”=>“Programme”}]}}}

Abinoam Jr.

On Mon, Jan 17, 2011 at 7:59 PM, Kamarulnizam R.

On Mon, Jan 17, 2011 at 4:59 PM, Kamarulnizam R.
[email protected]wrote:

 [{"name"=>"Training and Culture",

Posted via

I think working with this data will quickly become very difficult to
with. For an example, see the latest Peepcode ( where they spend
the screencast trying to figure out what their data looks like (its all
nested Hashes and Arrays, like yours). I think you should turn your data
into instances of some class[es] that defines how to deal with these
relationships in in a humane way.

Well, Arrays are ordered by nature. (Hashes shouldn’t be considered so).
That particular Hash was the first element of an Array.
So, it was the “0” element.


var = [ “first”, 2, { :third => 3, :not_third => true}]

var[0] #=> “first” (a String)
var[1] #=> 2 (a Fixnum)
var[2] #=> {:third=>3, :not_third=>nil} (a Hash)
var[2][:third] #=> 3 (The :third key of that Hash is pointing to the
Fixnum 3)
var[2][:not_third] #=> true

4000 lines?!

I strongly agree with Josh.
You should use another kind of data representation.
If you have no choice with the input data, you can (recursively) parse
it and transform it into something human readable.

Abinoam Jr.

On Mon, Jan 17, 2011 at 9:33 PM, Kamarulnizam R.

Hi Josh and Abinoam Jr,

Actually, those contents will be converted into YAML format for other
purpose where indentation is very important. And i dont think other kind
of data representation is suitable for that purpose. Anyway, what do you
mean by ‘recursively parse’ it? And where can i find any tutorials or
examples that use ‘that’, if by any chance available on the net?

Thanks in advance


Hi Abinoam,

I tried to change the children type from “programme” to “objective” by
using the following code:


but error came out. I used ‘1’ as i thought that hash is the second
element of an array (based on your previous post). Thanks


Hi Abinoam Jr,

How do you know the hash inside it is indexed 0? This is because the
rest of the outputs consist of nearly 4000 lines. Thus, locating
contents wil be very difficult if i dnt know how to find it. Thanks


In your example, there’s only one element in the Array, and 4 elements
in the inner Hash.


  1. Returns an Array.


#=> [{“name”=>“Training and Culture”, “type”=>“Objective”,
“subtype”=>“None”, “components”=>“None”}]

  1. Returns the first element of the Array. It’s the Hash you’re
    trying to manipulate.


=> {“name”=>“Training and Culture”, “type”=>“Objective”,

“subtype”=>“None”, “components”=>“None”}

  1. Now that you have the Hash, you can access its pairs (key,value).

#=> “Programme”

  1. Or, set it…

a[“System”][“H&S”][“children”][0][“type”] = “Objective”

Abinoam Jr.

On Mon, Jan 17, 2011 at 10:50 PM, Kamarulnizam R.