1

古いアプリを何度も修正した後、これが残っています

@products = {:parent_products=>[["Product title", "Product body 1", "3", "user1"], ["Product title 2", "Product body 2", "5", "user_2"]], :child_products=>[["making a reply", "user_2", "3", "4"], ["yes i read it", "user_5", "5", "6"], ["me too indeed. hurray", "user_1", "4", "7"], ["great to see this", "user_7", "3", "8"]]}

今、私がどうすればいいのかわからないのは-

  • 内部インデックスが配列自体として、その配列の最後のインデックスに追加される要素のいずれかの要素のいずれかに含まれている@products[:child_products]としましょう。@products[:child_products][2][2][3]

たとえば、上記のハッシュから、

の値は、(整数 4)@products[:child_products][2][2]で互いに似ています。@products[:child_products][0][3]それが満たされているのでtrue、新しい配列は次のようになります

["making a reply","user_2", "3", "4", ["me too indeed. hurray","user_1", "4", "7"]]

注意:上記は単なる例です。言い換えれば、検索内では@products[:child_products]、検索を見た目どおりに行う必要があります

@products[:child_products][<any index>][2] inside @products[:child_products][<any index>][3]

これまでのところ、私が理にかなっていることを願っています。

  • 先に進むと、条件が満たされていないか、(最初のポイントで述べたように) 満たされていても、内部の配列@products[:child_products]が再配置されている場合、別のロジックを実行する必要があります。

@products[:child_products][<any index>][2] should look for @product[:parent_products][<any index>][2]

そして、条件が満たされると、その配列全体をの配列に追加する必要があります@product[:parent_products]

たとえば、@product[:child_products][1][2]マッチ@product[:parent_products][1][2](5) の場合、内部の (それぞれのインデックス位置の) 新しい配列セットは次の@product[:parent_products]ようになります。

["Product title 2", "Product body 2", "5", "user_2",["yes i read it", "user_5", "5", "6"]]

それで全部です。

出力を明確に述べていることを確認するために最善を尽くしました。ご不明な点がございましたら、お問い合わせください。


更新:上記が混乱を引き起こしていることがわかりました。だからここに私が持っているものと私が欲しいものを一目見ます

私が持っているもの

@products = {:parent_products=>[["Product title", "Product body 1", "3", "user1"], ["Product title 2", "Product body 2", "5", "user_2"]], :child_products=>[["making a reply", "user_2", "3", "4"], ["yes i read it", "user_5", "5", "6"], ["me too indeed. hurray", "user_1", "4", "7"], ["great to see this", "user_7", "3", "8"]]}

そして、私が望む最終的な出力は、フォーマットされたデータを含む配列です

[["Product title", "Product body 1", "3", "user1", ["making a reply", "user_2", "3", "4", ["me too indeed. hurray", "user_1", "4", "7"]], ["great to see this", "user_7", "3", "8"] ], ["Product title 2", "Product body 2", "5", "user_2", ["yes i read it", "user_5", "5", "6"]]]

ありがとう

4

2 に答える 2

0
class Product
  attr_reader :description, :body, :id, :child_id

  def initialize description, body, id, child_id
    @description, @body, @id, @child_id = description, body, id, child_id
    @children = []
  end

  def append child
    @children << child
  end

  def accepts? child
    child.id == self.id
  end

  def to_a
    [description, body, id, child_id] + @children.map(&:to_a)
  end
end

class ChildProduct < Product
  def accepts? child
    self.child_id == child.id
  end
end

class ProductTransformer
  def initialize products
    @products = products
  end

  def transform
    parents  = @products[:parent_products].map{|e| Product.new *e}
    children = @products[:child_products].map{|e| ChildProduct.new *e}
    children.each do |child|
      p = parents.detect{|parent| parent.accepts? child}
      p.append child if p
      children.each do |another|
        next if another === child
        child.append another if child.accepts?(another)
      end
    end
    parents.map(&:to_a)
  end
end

products = {
  :parent_products=>[
    ["Product title",   "Product body 1", "3", "user1"],
    ["Product title 2", "Product body 2", "5", "user_2"]],
  :child_products=>[
    ["making a reply",        "user_2", "3", "4"], 
    ["yes i read it",         "user_5", "5", "6"], 
    ["me too indeed. hurray", "user_1", "4", "7"], 
    ["great to see this",     "user_7", "3", "8"]]
}
ProductTransformer.new(products).transform
于 2013-07-05T20:53:00.247 に答える