迭代sql结果以生成新的哈希数组

时间:2016-04-05 16:00:18

标签: ruby

我从SQL查询得到items的结果。我想使用这些结果来创建一个名为products的新哈希数组。我所走的道路似乎并不合适。迭代每一行时,我需要对行中的一些键进行条件处理,例如下面的伪代码。

items = retrieve_items_from_db

products = items.map do |row|
  {
    row.each do | k, v|
      if k == "Color" 
        #go do something and add updated key, value pair to products
      else puts "SKEY:#{k} VALUE: #{v}"
        # add k,v to products
         k => v
    end
  }
end

任何指针或建议?我尝试使用inject({})代替map

3 个答案:

答案 0 :(得分:0)

你在这里尝试做的事情很酷,但我们不能把条件陈述放在哈希中。

即。你做不到:

{
  if 2 == 2
    math: "correct"
  else
    math: "incorrect"
  end
}

你需要改为:

if 2 == 2
  { math: "correct" }
else
  { math: "incorrect" }
end

如果您需要将多个哈希合并为一个,Hash#merge方法很有用。

例如:

{ a: "a", b: "DEFAULT" }.merge({ b: "b", c: "c" })
# => { a: "a", b: "b", c: "c" }

您可以看到第二个哈希在第一个哈希中覆盖了值。

您可以在这里使用inject(或reduce,它可以做同样的事情,但听起来不那么可怕),但是因为您要将数组转换为数组,所以&#39 ; d更容易使用map

products = items.map do |row|
 if row.keys.include?(:price)
   row = row.merge(price: "#{row[:price]} dollars")
 else
   row = row.merge(price: "not available for sale")
 end
 next row
end

答案 1 :(得分:0)

使其成为嵌套映射,然后展平:

Tue Apr 05 15:09:34 2016
('1%  |                                                  |',)
('2%  |>                                                 |',)
('3%  |>                                                 |',)
('4%  |=>                                                |',)
('5%  |=>                                                |',)
('6%  |==>                                               |',)
('7%  |==>                                               |',)
('8%  |===>                                              |',)
('9%  |===>                                              |',)
('10% |====>                                             |',)
('11% |====>                                             |',)
('12% |=====>                                            |',)
('13% |=====>                                            |',)
('14% |======>                                           |',)
('15% |======>                                           |',)
('16% |=======>                                          |',)
('17% |=======>                                          |',)
('18% |========>                                         |',)
('19% |========>                                         |',)
('20% |=========>                                        |',)
('21% |=========>                                        |',)
('22% |==========>                                       |',)
('23% |==========>                                       |',)
('24% |===========>                                      |',)
('25% |===========>                                      |',)
('26% |============>                                     |',)
('27% |============>                                     |',)
('28% |=============>                                    |',)
('29% |=============>                                    |',)
('30% |==============>                                   |',)
('31% |==============>                                   |',)
('32% |===============>                                  |',)
('33% |===============>                                  |',)
('34% |================>                                 |',)
('35% |================>                                 |',)
('36% |=================>                                |',)
('37% |=================>                                |',)
('38% |==================>                               |',)
('39% |==================>                               |',)
('40% |===================>                              |',)
('41% |===================>                              |',)
('42% |====================>                             |',)
('43% |====================>                             |',)
('44% |=====================>                            |',)
('45% |=====================>                            |',)
('46% |======================>                           |',)
('47% |======================>                           |',)
('48% |=======================>                          |',)
('49% |=======================>                          |',)
('50% |========================>                         |',)
('51% |========================>                         |',)
('52% |=========================>                        |',)
('53% |=========================>                        |',)
('54% |==========================>                       |',)
('55% |==========================>                       |',)
('56% |===========================>                      |',)
('57% |===========================>                      |',)
('58% |============================>                     |',)
('59% |============================>                     |',)
('60% |=============================>                    |',)
('61% |=============================>                    |',)
('62% |==============================>                   |',)
('63% |==============================>                   |',)
('64% |===============================>                  |',)
('65% |===============================>                  |',)
('66% |================================>                 |',)
('67% |================================>                 |',)
('68% |=================================>                |',)
('69% |=================================>                |',)
('70% |==================================>               |',)
('71% |==================================>               |',)
('72% |===================================>              |',)
('73% |===================================>              |',)
('74% |====================================>             |',)
('75% |====================================>             |',)
('76% |=====================================>            |',)
('77% |=====================================>            |',)
('78% |======================================>           |',)
('79% |======================================>           |',)
('80% |=======================================>          |',)
('81% |=======================================>          |',)
('82% |========================================>         |',)
('83% |========================================>         |',)
('84% |=========================================>        |',)
('85% |=========================================>        |',)
('86% |==========================================>       |',)
('87% |==========================================>       |',)
('88% |===========================================>      |',)
('89% |===========================================>      |',)
('90% |============================================>     |',)
('91% |============================================>     |',)
('92% |=============================================>    |',)
('93% |=============================================>    |',)
('94% |==============================================>   |',)
('95% |==============================================>   |',)
('96% |===============================================>  |',)
('97% |===============================================>  |',)
('98% |================================================> |',)
Tue Apr 05 15:09:44 2016
('99% |================================================> |',)
('100%|=================================================>|',)

答案 2 :(得分:0)

我认为这种方式最简单:

items = retrieve_items_from_db

products = items.map do |row|
  product = {}
  row.each do | k, v|
    if k == "Color" 
      #go do something and add updated key, value pair to products
      product[transform_key(k)] = transform_value(v)
    else
      puts "SKEY:#{k} VALUE: #{v}"
      # add k,v to products
      product[k] = v
    end
  end
  product
end

您也可以使用Hash :: []将数组对的数组转换为Hash,但对我来说这很笨拙:

items = retrieve_items_from_db
products = items.map do |row|
  Hash[row.map do | k, v|
    if k == "Color" 
      #go do something and add updated key, value pair to products
      [transform_key(k), transform_value(v)]
    else
      puts "SKEY:#{k} VALUE: #{v}"
      # add k,v to products
      [k, v]
    end
  end]
end