如何在Ruby中统一数组?

时间:2010-08-13 06:32:55

标签: ruby mechanize

我知道“.uniq”方法,但它在这里不起作用。我在一个数组中推送了Mechanize链接实例并应用了它,但它没有删除重复项。这是阵列..

#<Mechanize::Page::Link "2" "/inquiry/inquiry-results.jsp?d-16544-p=2&middleName=&firstName=&lastName=JOHN">,
#<Mechanize::Page::Link "3" "/inquiry/inquiry-results.jsp?d-16544-p=3&middleName=&firstName=&lastName=JOHN">,
#<Mechanize::Page::Link "4" "/inquiry/inquiry-results.jsp?d-16544-p=4&middleName=&firstName=&lastName=JOHN">,
......................................................................................
#<Mechanize::Page::Link "2" "/inquiry/inquiry-results.jsp?d-16544-p=2&middleName=&firstName=&lastName=JOHN">,
#<Mechanize::Page::Link "3" "/inquiry/inquiry-results.jsp?d-16544-p=3&middleName=&firstName=&lastName=JOHN">,
#<Mechanize::Page::Link "4" "/inquiry/inquiry-results.jsp?d-16544-p=4&middleName=&firstName=&lastName=JOHN">,
#<Mechanize::Page::Link "5" "/inquiry/inquiry-results.jsp?d-16544-p=5&middleName=&firstName=&lastName=JOHN">,
#<Mechanize::Page::Link "6" "/inquiry/inquiry-results.jsp?d-16544-p=6&middleName=&firstName=&lastName=JOHN">,

但仍在使用之后。“.uniq”结果相同。这是我正在使用的红宝石代码..

page.links.each {|link| 
    page_links.push link if link.href =~ /inquiry-results/i and link.text =~ /[0-9]+/
}

1 个答案:

答案 0 :(得分:8)

Array#uniq使用对象的hasheql?方法。确保为您的对象正确定义了这些内容,或者根据您的条件使用uniq_by