0

我正在尝试减少代码中的重复。我在几个地方有这个代码(或其变体):

@articles1 = Article.all_articles(1).reverse
@articles2 = Article.all_articles(2).reverse
@articles3 = Article.all_articles(3).reverse

是一种将其更改为以下内容的方法:

3.times do |i|
  @articles[i+1] = Article.all_articles(i+1).reverse
end

干杯!

4

1 回答 1

1

怎么样:

@articles = (1..3).to_a.map { |i| Article.all_articles(i).reverse }
于 2012-11-29T02:09:06.987 回答