Adding contents on yaml file without overwriting actual contents

Hi,

By running the following codes:

convert_yaml = YAML::load_file(‘nizam.yaml’)
pp
convert_yaml[“System”][“Environmental”][“children”][2][“children”]
convert_yaml[“System”] = { “children” =>
[{“name”=>“nizam”,
“type”=>“Objective”,
“subtype”=>“None”,
“components”=>
[{“type”=>“ContentBox”,
“title”=>“Audit”,
“args”=>{:content=>“None\n”}},
{“type”=>“ChildListingComponent”,
“title”=>“Current Targets for the Audit Objective:”}]}]}
File.open(“nizam_out.yaml”, “w”){|f| YAML.dump(convert_yaml, f)}

My yaml output turns out to be like this:

System:
children:

  • name: nizam
    type: Objective
    subtype: None
    components:
    • type: ContentBox
      title: Audit
      args:
      :content: |
      None

    • type: ChildListingComponent
      title: “Current Targets for the Audit Objective:”

The actual yaml file before being converted is:

System:
H&S:
name: “Health & Safety”
components:
- FlashWheel
children:
- name: “Training and Culture”
type: Programme
subtype: None
components:
- type: ContentBox
title: “Training and Culture”
args:
:content: |
None
- type: ChildListingComponent
title: “Training and Culture Programme Objectives”
#Children named nizam should be added here
children:
- name: “Team”
type: Objective
subtype: None
components:
- type: ContentBox
title: “Team”
args:
:content: |
None
- type: ChildListingComponent
title: “Current Targets for the Team Objective:”

My question is how do i add children named ‘nizam’ under “Health &
Safety” without jeopardizing other contents (i.e “Training and Culture”
and further contents of H&S). I think YAML.dump in not the correct way
to do it. Thanks in advance.

Nizam

On 01/26/2011 09:58 PM, Kamarulnizam R. wrote:

       "subtype"=>"None",

System:

children:
    title: "Training and Culture Programme Objectives"
          None
    - type: ChildListingComponent
      title: "Current Targets for the Team Objective:"

My question is how do i add children named ‘nizam’ under “Health &
Safety” without jeopardizing other contents (i.e “Training and Culture”
and further contents of H&S). I think YAML.dump in not the correct way
to do it. Thanks in advance.

No, YAML.dump is doing what it should. The problem is that your code
replaces the value of the “System” key entirely with a new hash
containing a single key (“children”) whose value is a complex
combination of hashes and arrays. You effectively clobbered the hash
containing “H&S” key by doing that.

Because the children key appears in multiple levels in your original
data, I can’t say for sure where your new entry should go. You should
first figure out how to get a reference to the structure that should
contain your additional data. It may be one of the following:

convert_yaml[“System”][“H&S”][“children”]
convert_yaml[“System”][“H&S”][“children”][0][“children”]

Use pp to display each possibility until you find what you want. I
think you want the first one. In either case, the value will be an
array into which you will want to insert your new entry, if I understand
your needs correctly. Assuming the first possibility I listed, your
modification might go like this:

convert_yaml[“System”][“H&S”][“children”] << {
“name” => “nizam”,
“type” => “Objective”,
“subtype” => “None”,
“components” => [
{
“type” => “ContentBox”,
“title” => “Audit”,
“args” => {
:content => “None\n”
}
},
{
“type” => “ChildListingComponent”,
“title”=> “Current Targets for the Audit Objective:”
}
]
}

As a word of advice in general, you should try to simplify your tests
until you better understand what you’re trying to do. In this case,
make a much simplified YAML file to start with and play around with
modifying the data in irb or a stand alone test script. Then, as you
understand how to use your tools with your sample data, gradually add
more complexity to approach that of your actual data. What you’re doing
now is like jumping into the deep end of a pool without really knowing
how to swim. :slight_smile:

-Jeremy

On Thu, Jan 27, 2011 at 5:37 AM, Jeremy B. [email protected] wrote:

As a word of advice in general, you should try to simplify your tests
until you better understand what you’re trying to do. In this case,
make a much simplified YAML file to start with and play around with
modifying the data in irb or a stand alone test script. Then, as you
understand how to use your tools with your sample data, gradually add
more complexity to approach that of your actual data. What you’re doing
now is like jumping into the deep end of a pool without really knowing
how to swim. :slight_smile:

Also, Nizam, it would help the community enormously if you posted
within the same thread (forum topic) because otherwise someone
answering might miss all the other replies you have got so far. This
can be quite confusing.

Kind regards

robert

Jeremy, thanks for the post. Actually the key is ‘<<’ which is used to
add something. Now i am able to add contents inside my yaml file without
overwriting unnecessary contents. Actually, iam running out of time thus
testing using simplified yaml file might consume some time. Since my
yaml file consists of nearly 3000 lines, it is hard to simplified it.
but thanks for the advice. I definitely will implement it to my next
script

Robert, sorry for posting a lot of thread which might be confusing. I am
new to this forum so i dont how it works accordingly. i thought
different problems should on different thread even though it is based on
the same file. My bad. I will make sure it will never happen again in
near future.

Thanks guys!!

Nizam

On Fri, Jan 28, 2011 at 1:03 AM, Kamarulnizam R.
[email protected] wrote:

Robert, sorry for posting a lot of thread which might be confusing. I am
new to this forum so i dont how it works accordingly. i thought
different problems should on different thread even though it is based on
the same file. My bad. I will make sure it will never happen again in
near future.

Well, actually different problems should go to different threads but
the question is: what does “different” mean? In your case it seems
you want to solve one problem and have several difficulties, so it
seemed reasonable to treat them together. When creating different
threads, it’s good to provide at least some initial context in every
thread and not rely on people having read all the other topics.

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs