From: Jeremy Bopp Date: 2011-02-02T14:52:58+09:00 Subject: Re: remove array bracket On 02/01/2011 10:33 PM, Kamarulnizam Rahim wrote: > 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. ;-) > 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