0
lines = ["title= flippers dippers track= 9", "title= beaner bounce house track= 3", "title= fruit jams live track= 12"]
songs_formatted = []
songs = {}
lines.each do |line|
line =~ /title=\s?(.*)\s+t/
title = "#$1".strip
songs[:title] = title
line =~ /track=\s?(.*)/
track = "#$1".strip
songs[:track] = track
songs_formatted << songs
end
p songs_formatted
#=> [{:title=>"flippers dippers", :track=>"9"}]
#=> [{:title=>"beaner bounce house", :track=>"3"}, {:title=>"beaner bounce house", :track=>"3"}]
#=> [{:title=>"fruit jams live", :track=>"12"}, {:title=>"fruit jams live", :track=>"12"}, {:title=>"fruit jams live", :track=>"12"}]
每個連續的行覆蓋之前的行。爲什麼這不是按順序追加的?期望的結果是:如何將散列追加到數組?
songs_formatted = [{:title=>"flippers dippers", :track=>"9"}, {:title=>"beaner bounce house", :track=>"3"}, {:title=>"fruit jams live", :track=>"12"}]