Remove array bracket

Hi when i run my script, the output is as followed:

[{"name"=>"Efficiency (Energy)",
  "type"=>"Objective",
  "subtype"=>"NIZAMMMMzz",
  "components"=>
   [{"type"=>"ContentBox", "title"=>"Audit",
"args"=>{:content=>"None\n"}},
    {"type"=>"ChildListingComponent",
     "title"=>"Current Targets for the Efficiency (Energy)
Objective:"}]},
{"children"=>
   [{"name"=>"Routinely monitor energy use",
     "type"=>"Target",
     "subtype"=>"ATEEN",
     "components"=>
      [{"type"=>"ContentBox",
        "title"=>"Summary",
        "args"=>{:content=>"None\n"}},
       {"type"=>"ContentBox", "title"=>"Description",
"args"=>{:content=>""}},
       {"type"=>"TargetComponent",
        "title"=>"None",
        "args"=>
         {:start_date=>"#<Date: 2010-08-10 ",
          :end_date=>"5/1/2011",
          :state=>"Active"}}]}]},
...........
...........
          :state=>"Active"}}]}]}]

How do i tell ruby to remove the very first and the very last bracket
'[...]' out from my output?

Nizam

···

--
Posted via http://www.ruby-forum.com/.

Are these output to a txt file, or irb? Mind sharing your output
formatting code?

···

--
Posted via http://www.ruby-forum.com/.

How does your script produce that output in the first place? I'm going
to assume that you have a reference to an array and that you're printing
the output of that array's inspect method. In that case, you could try
something like this:

array = ... # Array value from somewhere
puts array.map(&:inspect).join(", ")

This code maps each element of the array to the value of its associated
inspect method and then joins all elements of the array with ", ". You
should be able to use similar logic if you are using something other
than the inspect method as well.

-Jeremy

···

On 02/01/2011 06:50 PM, Kamarulnizam Rahim wrote:

Hi when i run my script, the output is as followed:

[{"name"=>"Efficiency (Energy)",
  "type"=>"Objective",
  "subtype"=>"NIZAMMMMzz",
  "components"=>
   [{"type"=>"ContentBox", "title"=>"Audit",
"args"=>{:content=>"None\n"}},
    {"type"=>"ChildListingComponent",
     "title"=>"Current Targets for the Efficiency (Energy)
Objective:"}]},
{"children"=>
   [{"name"=>"Routinely monitor energy use",
     "type"=>"Target",
     "subtype"=>"ATEEN",
     "components"=>
      [{"type"=>"ContentBox",
        "title"=>"Summary",
        "args"=>{:content=>"None\n"}},
       {"type"=>"ContentBox", "title"=>"Description",
"args"=>{:content=>""}},
       {"type"=>"TargetComponent",
        "title"=>"None",
        "args"=>
         {:start_date=>"#<Date: 2010-08-10 ",
          :end_date=>"5/1/2011",
          :state=>"Active"}}]}]},
...........
...........
          :state=>"Active"}}]}]}]

How do i tell ruby to remove the very first and the very last bracket
'[...]' out from my output?

Hi guys,

Actually i just want to remove the bracket (The very first and last
bracket) because if the bracket still there, it will affect the
indentation on my yaml output (as the posted output above will be
converted into yaml format). This is what will happen on my yaml output
if i do not remove the brackets:

  - - name: Efficiency (Energy)
          type: Objective
          subtype: NIZAMMMMzz
          components:
          - type: ContentBox
            title: Audit
            args:
              :content: |
                None

          - type: ChildListingComponent
            title: "Current Targets for the Efficiency (Energy)
Objective:"
        - children:
          - name: Routinely monitor energy use
            type: Target
            subtype: ATEEN
            components:
            - type: ContentBox
              title: Summary
              args:
                :content: |
                  None

As you can see, there are double indented dash (- -) on the Efficiency
(energy) and i want to remove one of those.This is the code i use:

    tar = [ ]
    objective = {"name"=>"#{blank}",
           "type"=>"Objective",
           "subtype"=>"NIZAMMMMzz",
           "components"=>
            [{"type"=>"ContentBox",
              "title"=>"Audit",
              "args"=>{:content=>"None\n"}},
             {"type"=>"ChildListingComponent",
              "title"=>"Current Targets for the #{blank} Objective:"}]}
     tar << objective

     mon = { "children"=>
      [{"name"=>"#{question}",
              "type"=>"Target",
              "subtype"=>"ATEEN",
              "components"=>
               [{"type"=>"ContentBox",
                 "title"=>"Summary",
                 "args"=>{:content=>"None\n"}},
                {"type"=>"ContentBox",
                 "title"=>"Description",
                 "args"=>
                  {:content=>
                    ""}},
                {"type"=>"TargetComponent",
                 "title"=>"None",
                 "args"=>
                  {:start_date=>"#<Date: 2010-08-10 ",
                   :end_date=>"#{due_date}",
                   :state=>"#{status}"}}]}]}
     tar << mon
     pp tar

Nizam

···

--
Posted via http://www.ruby-forum.com/.

class EnergyManagement < Environmental
  def initialize(title)
    @title = title
  end
  def objective
    convert_yaml = Environmental.yaml #YAML::load_file('nizam.yaml')
    convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title
    File.open("nizam_out.yaml", "w"){|f| YAML.dump(convert_yaml, f)}
  end
end

e = EnergyManagement.new(tar)
e.objective

···

--
Posted via http://www.ruby-forum.com/.

class Environmental
   @@convert_yaml = nil
   def self.yaml
     if @@convert_yaml.nil? then
  @@convert_yaml = YAML::load_file('nizam.yaml')
     end
     @@convert_yaml
   end
end

···

------------------------------------------------------------------
      - name: Optimisation
        type: Objective
        subtype: None
        components:
        - type: ContentBox
          title: Optimisation
          args:
            :content: |
              None

        - type: ChildListingComponent
          title: "Current Targets for the Optimisation Objective:"
-------------------------------------------------------------------
      - - name: Efficiency (Energy)
          type: Objective
          subtype: NIZAMMMzzz
          components:
          - type: ContentBox
            title: Team
            args:
              :content: |
                None

the input of my yaml file is similar as data within the dashed line. The
thing is, by eliminating the brackets as i mentioned earlier will
eliminate the double dashes (- - name: Efficiency (Energy)), hence
corrected the indentation on my yaml output file. I just dnt know how to
eliminate that bracket since i added two hashes ('objective' and 'mon')
into an array ('tar'). Is there any other way i can add those two hashes
without inserting it into an array? My desire yaml output file is like
this:

      - name: Optimisation
        type: Objective
        subtype: None
        components:
        - type: ContentBox
          title: Optimisation
          args:
            :content: |
              None

        - type: ChildListingComponent
          title: "Current Targets for the Optimisation Objective:"

      - name: Efficiency (Energy)
        type: Objective
        subtype: NIZAMMMzzz
        components:
        - type: ContentBox
          title: Team
          args:
            :content: |
              None

Thanks

Nizam

--
Posted via http://www.ruby-forum.com/.

Thanks Jeremy,

I am able to solve the problem regarding indentation by changing the
code as posted. There is one little question i want to ask.

How do convert these two hashes:

[{xxxxxx},{yyyyyy}]

to be like this:

[{xxxxxx, yyyyyy}]

Is it possible at all?

Regarding the Environmental class. I still need that class because i
want to open my yaml file to be used by many subclasses, not just
EnergyManagement only. Thanks

Nizam

···

--
Posted via http://www.ruby-forum.com/.

Btw there is a catch, {yyyyyy} hash is created based on a looping method
(CSV.foreach) and i dont think using common inject, collect or merge
method is the right choice. I tried it, and it didnt work

Nizam

···

--
Posted via http://www.ruby-forum.com/.

Hi Jeremy,

I have already solved the problem by myself by manipulating bit of the
hash content. Its quite easy though. Maybe i think of the wrong way
before this. If you are still interested, i can show you how i done it.
Cheers

Nizam

···

--
Posted via http://www.ruby-forum.com/.

The variable tar is an array of hashes, and the first output you posted
agrees with this. At this point there does not appear to be any cause
for an added array wrapping tar. You need to show us how you output the
value of tar into your YAML file. You are likely wrapping tar within
another array before you do that.

-Jeremy

···

On 02/01/2011 08:22 PM, Kamarulnizam Rahim wrote:

Hi guys,

Actually i just want to remove the bracket (The very first and last
bracket) because if the bracket still there, it will affect the
indentation on my yaml output (as the posted output above will be
converted into yaml format). This is what will happen on my yaml output
if i do not remove the brackets:

  - - name: Efficiency (Energy)
          type: Objective
          subtype: NIZAMMMMzz
          components:
          - type: ContentBox
            title: Audit
            args:
              :content: |
                None

          - type: ChildListingComponent
            title: "Current Targets for the Efficiency (Energy)
Objective:"
        - children:
          - name: Routinely monitor energy use
            type: Target
            subtype: ATEEN
            components:
            - type: ContentBox
              title: Summary
              args:
                :content: |
                  None

As you can see, there are double indented dash (- -) on the Efficiency
(energy) and i want to remove one of those.This is the code i use:

    tar =
    objective = {"name"=>"#{blank}",
           "type"=>"Objective",
           "subtype"=>"NIZAMMMMzz",
           "components"=>
            [{"type"=>"ContentBox",
              "title"=>"Audit",
              "args"=>{:content=>"None\n"}},
             {"type"=>"ChildListingComponent",
              "title"=>"Current Targets for the #{blank} Objective:"}]}
     tar << objective

     mon = { "children"=>
      [{"name"=>"#{question}",
              "type"=>"Target",
              "subtype"=>"ATEEN",
              "components"=>
               [{"type"=>"ContentBox",
                 "title"=>"Summary",
                 "args"=>{:content=>"None\n"}},
                {"type"=>"ContentBox",
                 "title"=>"Description",
                 "args"=>
                  {:content=>
                    ""}},
                {"type"=>"TargetComponent",
                 "title"=>"None",
                 "args"=>
                  {:start_date=>"#<Date: 2010-08-10 ",
                   :end_date=>"#{due_date}",
                   :state=>"#{status}"}}]}]}
     tar << mon
     pp tar

At this point I can't see where the problem is since none of this
correlates in an obvious way with your earlier posts. I think you need
to simplify this whole thing into a basic test case using a stripped
down and much simplified YAML file that reproduces the core problem.
That way those of us here can try out your code, see exactly what you
see, and ask you more intelligent question. :slight_smile:

-Jeremy

···

On 02/01/2011 09:04 PM, Kamarulnizam Rahim wrote:

class EnergyManagement < Environmental
  def initialize(title)
    @title = title
  end
  def objective
    convert_yaml = Environmental.yaml #YAML::load_file('nizam.yaml')
    convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title
    File.open("nizam_out.yaml", "w"){|f| YAML.dump(convert_yaml, f)}
  end
end

e = EnergyManagement.new(tar)
e.objective

What does the Environmental class look like?

And what does the input YAML look like?

···

On Wed, Feb 2, 2011 at 4:04 AM, Kamarulnizam Rahim <niezam54@hotmail.com> wrote:

class EnergyManagement < Environmental

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

class Environmental
   @@convert_yaml = nil
   def self.yaml
     if @@convert_yaml.nil? then
  @@convert_yaml = YAML::load_file('nizam.yaml')
     end
     @@convert_yaml
   end
end

Given the way you used this class within the EnergyManagement class,
there is no need to make EnergyManagement inherit from Environmental.
Just FYI.

------------------------------------------------------------------
      - name: Optimisation
        type: Objective
        subtype: None
        components:
        - type: ContentBox
          title: Optimisation
          args:
            :content: |
              None

        - type: ChildListingComponent
          title: "Current Targets for the Optimisation Objective:"
-------------------------------------------------------------------
      - - name: Efficiency (Energy)
          type: Objective
          subtype: NIZAMMMzzz
          components:
          - type: ContentBox
            title: Team
            args:
              :content: |
                None

the input of my yaml file is similar as data within the dashed line. The
thing is, by eliminating the brackets as i mentioned earlier will
eliminate the double dashes (- - name: Efficiency (Energy)), hence
corrected the indentation on my yaml output file. I just dnt know how to
eliminate that bracket since i added two hashes ('objective' and 'mon')
into an array ('tar').

Let's fix some terminology here just to prevent potential confusion.
You don't want to eliminate brackets. You want to extract these items
from the array in which they are embedded. "Eliminating brackets" is
something you would do directly to the string output generated by
something like the inspect method. The apparent extra array is the root
of the problem rather than the brackets shown when you inspect it. :wink:

Is there any other way i can add those two hashes
without inserting it into an array? My desire yaml output file is like
this:

      - name: Optimisation
        type: Objective
        subtype: None
        components:
        - type: ContentBox
          title: Optimisation
          args:
            :content: |
              None

        - type: ChildListingComponent
          title: "Current Targets for the Optimisation Objective:"

      - name: Efficiency (Energy)
        type: Objective
        subtype: NIZAMMMzzz
        components:
        - type: ContentBox
          title: Team
          args:
            :content: |
              None

OK. It's really hard to be sure here, but I think the problem is that
you need to convert this section of your code:

convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title

To this:

convert_yaml["System"]["Environmental"]["children"][2]["children"] += @title

The issue is that tar is an array of hashes, but it appears that you
actually want to append the contents of tar to the data structure you
have from the YAML file instead of appending tar itself. This code
appends the contents of the array in tar to the existing array in that
mess of hashes.

There are other ways to handle this, but this one is the simplest
change. The downside is that you would prevent someone actually
appending tar as-is if they actually wanted to do so. Maybe that's not
an issue for this code.

-Jeremy

···

On 02/01/2011 10:33 PM, Kamarulnizam Rahim wrote:

Thanks Jeremy,

I am able to solve the problem regarding indentation by changing the
code as posted. There is one little question i want to ask.

How do convert these two hashes:

[{xxxxxx},{yyyyyy}]

to be like this:

[{xxxxxx, yyyyyy}]

Is it possible at all?

Check out Hash#merge. Assuming you have an array of hashes in the
variable arr, you could merge them all like this:

arr.inject({}, &:merge)

Give that a try in irb and see if you can fit it into your code.

Regarding the Environmental class. I still need that class because i
want to open my yaml file to be used by many subclasses, not just
EnergyManagement only. Thanks

I'm not saying that you don't need the Environmental class at all. You
just don't need to make the EnergyManagement class inherit from it in
order to use it the way you are in the code. Calling Environmental.yaml
will still work.

-Jeremy

···

On 2/2/2011 1:53 AM, Kamarulnizam Rahim wrote:

I'm not sure what you're saying here. If you're saying you need some
custom merge logic that takes into account your data structures, you're
going to have to provide some examples that demonstrate the issue.

Please try to keep the examples as succinct as possible. Your real data
is quite complex, and trying to make sense of it in this forum is a real
challenge.

-Jeremy

···

On 2/2/2011 1:59 AM, Kamarulnizam Rahim wrote:

Btw there is a catch, {yyyyyy} hash is created based on a looping method
(CSV.foreach) and i dont think using common inject, collect or merge
method is the right choice. I tried it, and it didnt work

What's important is that you fixed your problem and that you understand
how you did it. If you have more questions, please post them though.

-Jeremy

···

On 2/2/2011 3:53 PM, Kamarulnizam Rahim wrote:

Hi Jeremy,

I have already solved the problem by myself by manipulating bit of the
hash content. Its quite easy though. Maybe i think of the wrong way
before this. If you are still interested, i can show you how i done it.

OK. It's really hard to be sure here, but I think the problem is that
you need to convert this section of your code:

convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title

To this:

convert_yaml["System"]["Environmental"]["children"][2]["children"] += @title

I'd rather

convert_yaml["System"]["Environmental"]["children"][2]["children"].concat(@title)

The issue is that tar is an array of hashes, but it appears that you
actually want to append the contents of tar to the data structure you
have from the YAML file instead of appending tar itself. This code
appends the contents of the array in tar to the existing array in that
mess of hashes.

There are other ways to handle this, but this one is the simplest
change. The downside is that you would prevent someone actually
appending tar as-is if they actually wanted to do so. Maybe that's not
an issue for this code.

Another remark: I don't understand why indentation in a YAML file is
such an issue. After all it's a data format and readable either way.
Typically too few indentation is more an issue than too much. If the
formatting is that crucial it might be worthwhile to look into writing
a new YAML formatter.

Cheers

robert

···

On Wed, Feb 2, 2011 at 6:52 AM, Jeremy Bopp <jeremy@bopp.net> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I think there is confusion here because of the way Nizam phrased the
questions. The indentation is symptomatic of having too much array
nesting for the intended data structure. The formatting itself wasn't
the problem.

-Jeremy

···

On 02/02/2011 02:52 AM, Robert Klemme wrote:

Another remark: I don't understand why indentation in a YAML file is
such an issue. After all it's a data format and readable either way.
Typically too few indentation is more an issue than too much. If the
formatting is that crucial it might be worthwhile to look into writing
a new YAML formatter.