简体   繁体   English

rubocop字符串内插和大小条件

[英]rubocop string interpolation and size condition

在此处输入图片说明

Before I except these two methods I wanted to see if anyone in the community had a better idea to structure these and make the cops pass. 除了这两种方法之外,我想先看看社区中是否有人有更好的主意来构造这两种方法并使警察通过。 The first one with to_s seems a bit crazy too. 第一个带有to_s似乎也有些疯狂。 I was thinking of refactoring the other method but that would be a single line or two. 我当时正在考虑重构另一种方法,但这将是一两行。

Thoughts? 有什么想法吗?

Code Examples One: 代码示例一:

  def destroy(resource_name, id)
    delete "#{resource_name.to_s.pluralize}/#{id}"
  end

Code Examples Two: 代码示例二:

  def all_products
    products_map = fetch(:products).map { |x| [x['id'], x] }.to_h
    variants = fetch :variants
    variants.group_by { |x| x['product']['resource']['id'] }.to_a.map do |product_id, product_variants|
      product.merge 'variants' => product_variants if product == products_map[product_id]
    end.compact
  end

For Code example One, maybe this can be used: 对于代码示例一,也许可以使用:

delete [resource_name.to_s.pluralize, id].join('/')  

For Code example Two, yes you definitely need to refactor it. 对于代码示例2,是的,您肯定需要对其进行重构。
Maybe you need to create a separate method that does all the grouping and merging, etc. for the variants part. 也许您需要创建一个单独的方法来对variants部分进行所有分组和合并等操作。
I am not sure if this is a good practice, but you can create a private method for it. 我不确定这是否是一个好习惯,但是您可以为其创建私有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM