0

我正在尝试使用 Ruby 的 IMAP 库来获取所有电子邮件发件人的列表(“发件人”),然后按字母顺序对其进行排序,然后计算每个人的电子邮件数量。

我正在第 1 步挂断 - 按字母顺序排序。这是我拥有的代码,它返回所有“来自”值的列表,但它们绝对不是按字母顺序排列的。

完整的红宝石新手 - 不到 1 周,所以请保持温和。

mail_count = imap.search(["SINCE", @this_week.strftime("%d-%b-%Y")]).each do |message_id|
  envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
from_array = envelope.from[0].name.to_a
sorted_from = from_array.sort 
puts "#{sorted_from}"
end
4

1 回答 1

0

也许是这样:

results = []
mail_count = imap.search(["SINCE", @this_week.strftime("%d-%b-%Y")]).each do |message_id|
  envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
  from_array = envelope.from[0].name.to_a
  results << from_array
end
results.sort.each do |el|
  puts "#{el}"
end
于 2012-02-22T05:31:14.007 回答