gpt4 book ai didi

ruby - 如何在 Ruby 中唯一化数组?

转载 作者:数据小太阳 更新时间:2023-10-29 08:21:39 24 4
gpt4 key购买 nike

我知道“.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”后结果仍然相同。这是我正在使用的 ruby 代码..

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

最佳答案

Array#uniq 使用对象的 hasheql? 方法。确保为您的对象正确定义了这些,或者将 uniq_by 与您的标准一起使用。

关于ruby - 如何在 Ruby 中唯一化数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3474445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com