使用可枚举#select创建特定数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用可枚举#select创建特定数组相关的知识,希望对你有一定的参考价值。
Useful for when you need to do a find and map specific results to an array for use later
#Instead of using: books = Book.find(:all, :order => 'id desc', :limit => 20) comics = [] books.each do |book| comics << book if book.item_type == 'Comic' end #OR comics = Book.find(:all, :order => 'id desc', :limit => 20).map {|book| [book] unless book.item_type == 'Comic'}.flatten! #Use this: books = Book.find(:all, :order => 'id desc', :limit => 20) comics = books.select { |i| i.item_type == 'Comic' }
以上是关于使用可枚举#select创建特定数组的主要内容,如果未能解决你的问题,请参考以下文章