Re: Ruby Quiz - Challenge #4 - Turn Humanitarian eXchange Language (HXL) Tabular Records into Named Tuples

"Frank J. Cameron" <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
Gerald Bauer wrote:
>   Challenge #4 - Turn Humanitarian eXchange Language (HXL) Tabular
> Records into Named Tuples

$ ruby ./lib/004.rb
Run options: --seed 28092
# Running:
.F
Finished in 0.005780s, 346.0336 runs/s, 346.0336 assertions/s.
  1) Failure:
RubyQuizTest#test_level2 [/home/fjc/ruby-quiz/004/test.rb:71]:
--- expected
+++ actual
@@ -1 +1 @@
-[{"sector+en"=>"WASH", "subsector"=>"Subsector 1", "org"=>"Org 1",
"country"=>"Country 1", "sex+targeted"=>["100", "100"], "adm1"=>"Region
1"}, {"sector+en"=>"Health", "subsector"=>"Subsector 2", "org"=>"Org 2",
"country"=>"Country 2", "sex+targeted"=>["", ""], "adm1"=>"Region 2"},
{"sector+en"=>"Education", "subsector"=>"Subsector 3", "org"=>"Org 3",
"country"=>"Country 2", "sex+targeted"=>["250", "300"], "adm1"=>"Region
3"}, {"sector+en"=>"WASH", "subsector"=>"Subsector 4", "org"=>"Org 1",
"country"=>"Country 3", "sex+targeted"=>["80", "95"], "adm1"=>"Region 4"}]
+[{"sector+en"=>"WASH", "subsector"=>"Subsector 1", "org"=>"Org 1",
"country"=>"Country 1", "sex+targeted"=>["100", "100"], "adm1"=>"Region
1"}, {"sector+en"=>"Health", "subsector"=>"Subsector 2", "org"=>"Org 2",
"country"=>"Country 2", "sex+targeted"=>["", ""], "adm1"=>"Region 2"},
{"sector+en"=>"Education", "subsector"=>"Subsector 3", "org"=>"Org 3",
"country"=>"Country 2", "sex+targeted"=>["250,300", "Region 3"],
"adm1"=>nil}, {"sector+en"=>"WASH", "subsector"=>"Subsector 4",
"org"=>"Org 1", "country"=>"Country 3", "sex+targeted"=>["80", "95"],
"adm1"=>"Region 4"}]
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips

$ cat ./lib/004.rb
require_relative '../004/test.rb'
class RubyQuizTest
  def parse(recs)
    header_index = recs.find.with_index do |rec, i|
      break i if rec.any?{|r| r[0] == ?# }
    end
    header_rec = recs[header_index].map do |r|
      r.delete!(?#)
    end
    recs[header_index+1..-1].map do |rec|
      {}.tap do |rec_hash|
        header_rec.zip(rec) do |(h,r)|
          next unless h
          if rec_hash.has_key?(h)
            if rec_hash[h].instance_of?(Array)
              rec_hash[h] << r
            else
              rec_hash[h] = [rec_hash[h]] << r
            end
          else
            rec_hash[h] = r
          end
        end
      end
    end
  end
end
RubyQuizTest.new('fjc')



Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.