How to DRY up method with multiple { 'not found' }?

后端 未结 2 418
情话喂你
情话喂你 2021-01-16 22:36

I\'m trying to gracefully handle bad JSON for the following, where Hash#fetch doesn\'t seem to be an option (Handle bad JSON gracefully with Hash#fetch):

<
2条回答
  •  太阳男子
    2021-01-16 23:30

    I not sure about right syntax in product.price = product.deep_fetch(attribute, { 'not found' }):

    mashie.products.each do |product|
      product.extend Hashie::Extensions::DeepFetch
    
      i%[name sale_price currency].each do |attribute|
        product.price = product.deep_fetch attribute { 'not found' }
      end
    
      @products << product
    end
    

提交回复
热议问题