首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用迭代和推入数组重构Ruby If/Else

使用迭代和推入数组重构Ruby If/Else
EN

Stack Overflow用户
提问于 2018-12-14 22:52:06
回答 3查看 68关注 0票数 0

我正在尝试清理一个遍历推入数组的数据的方法。大概是这样的:

代码语言:javascript
复制
def sort_by_title(authors)
 general = []
 fiction = []
 factual = []
 food = []
 other = []

 authors.each do |a|
  if a.tag.include?('General')
   general << a
  elsif a.tag.include?('Historical')
    fiction << a if a.tag.include?('iction')
    factual <<a if a.tag.include?('actual')
  elseif a.tag.include?('Food')
    food << a
  else
    other << a
  end
 end
 (general + fiction + factual + food + other).flatten
end
end

鲁博科普正在用:Metrics/AbcSize and Metrix/Perceived Complexity攻击我。有没有更干净的方法可以这样做呢?

EN

回答 3

Stack Overflow用户

发布于 2018-12-14 23:32:42

我会选择case

代码语言:javascript
复制
authors.each_with_object(Hash.new { |h, k| h[k] = [] }) do |a, hash|
  case a.tag
  when /General/ then h[:general] << a
  when /Historical.*iction/ then h[:fiction] << a
  when /Historical.*actual/ then h[:factual] << a
  when /Food/ then h[:food] << a
  else h[:other] << a
end.values.flatten

或者,使用Enumerable#sort_by使用更令人兴奋和语义正确的方法

代码语言:javascript
复制
authors.sort_by do |a|
  [
    10 if a.tag.include?('General'),
    if a.tag.include?('iction')
      8
    elsif if a.tag.include?('actual')
      6
    end if a.tag.include?('Historical'),
    4 if a.tag.include?('Food'),
    2
  ].compact.sum
end
票数 2
EN

Stack Overflow用户

发布于 2018-12-14 23:16:28

您可以使用#group_by

代码语言:javascript
复制
def sort_by_title(authors)
  grouped = authors.group_by do |a| 
    if a.tag.include?('General')
      :general
    elsif a.tag.include?('Historical')
      :fiction if a.tag.include?('iction')
      :factual if a.tag.include?('actual')
    elsif a.tag.include?('Food')
      :food
    else
      :other
    end
  end
  grouped.values.flatten
end

编辑:

为了让它更清晰,你可以提取关于流派的方法:

代码语言:javascript
复制
class Author
  def genre
    if tag.include?('General')
      :general
    elsif tag.include?('Historical')
      :fiction if tag.include?('iction')
      :factual if tag.include?('actual')
    elsif tag.include?('Food')
      :food
    else
      :other
    end
  end
end

def sort_by_title(authors)
  authors.group_by(&:genre).values.flatten
end
票数 1
EN

Stack Overflow用户

发布于 2018-12-15 07:41:20

您需要对它们进行分组还是排序?如果只是排序,是否可以使用带有查找字段的sort_by

代码语言:javascript
复制
TAGS = {
  'General' => 1,
  'Fiction' => 2,
  'Factual' => 3,
  'Food' => 4,
  'Other' => 5
}

authors = [
  { name: 'Joe', tag: 'General' },
  { name: 'Sue', tag: 'Fiction' },
  { name: 'Sally', tag: 'Food' },
  { name: 'Oliver', tag: 'Factual' },
  { name: 'Bob', tag: 'Other' },
  { name: 'Billy', tag: 'General' }
]

sorted_authors =
  authors.sort_by do |author|
    TAGS[author[:tag]] # This would be author.tag in your example
  end

puts sorted_authors

收益率

代码语言:javascript
复制
{:name=>"Joe", :tag=>"General"}
{:name=>"Billy", :tag=>"General"}
{:name=>"Sue", :tag=>"Fiction"}
{:name=>"Oliver", :tag=>"Factual"}
{:name=>"Sally", :tag=>"Food"}
{:name=>"Bob", :tag=>"Other"}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53781991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档