Objective:"}]},
{“type”=>“TargetComponent”,
‘[…]’ out from my output?
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.
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:
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
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.
On Wed, Feb 2, 2011 at 4:04 AM, Kamarulnizam R. [email protected] wrote:
class EnergyManagement < Environmental
What does the Environmental class look like?
And what does the input YAML look like?
–
Phillip G.
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
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:
As you can see, there are double indented dash (- -) on the Efficiency
{“type”=>“ChildListingComponent”,
“args”=>{:content=>“None\n”}},
:state=>"#{status}"}}]}]}
tar << mon
pp tar
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.
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
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
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.
: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.
args:
- 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:
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.
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.
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.
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.
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.
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
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.
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.