Adding contents on yaml file without overwriting actual contents

Discussion in 'Ruby' started by Kamarulnizam Rahim, Jan 27, 2011.

  1. 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

    --
    Posted via http://www.ruby-forum.com/.
    Kamarulnizam Rahim, Jan 27, 2011
    #1
    1. Advertising

  2. Kamarulnizam Rahim

    Jeremy Bopp Guest

    On 01/26/2011 09:58 PM, Kamarulnizam Rahim wrote:
    > 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.


    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. :)

    -Jeremy
    Jeremy Bopp, Jan 27, 2011
    #2
    1. Advertising

  3. On Thu, Jan 27, 2011 at 5:37 AM, Jeremy Bopp <> 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. =A0In 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. =A0Then, as you
    > understand how to use your tools with your sample data, gradually add
    > more complexity to approach that of your actual data. =A0What you're doin=

    g
    > now is like jumping into the deep end of a pool without really knowing
    > how to swim. :)


    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

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jan 27, 2011
    #3
  4. 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

    --
    Posted via http://www.ruby-forum.com/.
    Kamarulnizam Rahim, Jan 28, 2011
    #4
  5. On Fri, Jan 28, 2011 at 1:03 AM, Kamarulnizam Rahim
    <> 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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jan 28, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stewart
    Replies:
    8
    Views:
    791
    Stewart
    Nov 5, 2004
  2. Steven D'Aprano

    Safely renaming a file without overwriting

    Steven D'Aprano, Oct 28, 2006, in forum: Python
    Replies:
    11
    Views:
    461
    Wolfgang Draxinger
    Oct 29, 2006
  3. Bart  Kastermans
    Replies:
    3
    Views:
    510
    Bart Kastermans
    Jan 25, 2008
  4. RubyQuestions
    Replies:
    0
    Views:
    219
    RubyQuestions
    Dec 3, 2003
  5. Paul Battley

    YAML.dump/YAML.load bug

    Paul Battley, Aug 3, 2005, in forum: Ruby
    Replies:
    0
    Views:
    176
    Paul Battley
    Aug 3, 2005
Loading...

Share This Page