0

したがって、次のハッシュ/配列があります。

{"number"=>[{"tracking"=>"1Z81E74W0393736553", "notes"=>"Example note"}, {"tracking"=>"9102901001301227214058"}]}

{"number"=>{"tracking"=>"1Z81E74W0393736553", "notes"=>"Example note"}}

最初のハッシュには配列がnumberあり、2 番目のハッシュにはありません。

データをループしようとして大混乱を引き起こしています (特に、トラッキングとノートの組み合わせが 1 つしかない場合)。

each最終的には、各トラッキング/ノート コンボでループを実行できるようにしたいと考えています。

4

2 に答える 2

2
h1={"number"=>[{"tracking"=>"1Z81E74W0393736553", "notes"=>"Example note"}, {"tracking"=>"9102901001301227214058"}]}
h2={"number"=>{"tracking"=>"1Z81E74W0393736553", "notes"=>"Example note"}}
[h1["number"]].flatten
  => [{"notes"=>"Example note", "tracking"=>"1Z81E74W0393736553"}, {"tracking"=>"9102901001301227214058"}]
[h2["number"]].flatten
  => [{"notes"=>"Example note", "tracking"=>"1Z81E74W0393736553"}]

これで、それぞれがハッシュの配列になり、eachそれらを反復処理するために使用できます。

于 2010-03-19T19:10:59.033 に答える
1

このようなもの?

hash["number"] = [ hash["number"] ] unless hash["number"].kind_of?(Array)
于 2010-03-19T19:06:33.983 に答える