鉴于我有类似以下的代码,我需要做些什么才能使它工作?如何创建一个按需求出另一个可枚举对象的可枚举对象?
config = {} #options for faster csv
input_file = "foo.csv"
# can be in any class or module
def count_item_groups(items)
results = Hash.new(0)
(items || []).each do |current|
results[current.to_s] += 1
end
results
end
row_value_iterator = FasterCSV.foreach(input_file, config) do |row|
yield return row[1]
end
result = count_item_groups(row_value_iterator)
对战这样的代码
def do_it_all
results = Hash.new(0)
FasterCSV.foreach(input_file, config) do |row|
results[row[1].to_s] += 1
end
results
end
结果应与行[1]的值的密钥的散列。 Ruby中不存在yield return
,但我确定Ruby可以处理这种类型的代码。
什么是收益率回归行[1]该怎么办?和什么是C#标签? – oldergod
是的。 http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx我想将所创建的迭代器(row_value_iterator)中的行值逐个1放入到count_item_groups方法中。 – Jason
count_item_groups正在调用.each集合。我想这样做是为了将知道CSV文件中哪些列的代码从将执行分组的代码中分离出来。 – Jason