gpt4 book ai didi

ruby - ruby 数组的 uniq 无法工作

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

我有一个对象 Country 的数组,它具有属性“code”和“name”

数组中可能有一个国家不止一次,所以我想区分数组。

这是我的国家类

class Country
include Mongoid::Fields::Serializable
attr_accessor :name, :code

FILTERS = ["Afghanistan","Brunei","Iran", "Kuwait", "Libya", "Saudi Arabia", "Sudan", "Yemen", "Britain (UK)", "Antarctica", "Bonaire Sint Eustatius & Saba", "British Indian Ocean Territory", "Cocos (Keeling) Islands", "St Barthelemy", "St Martin (French part)", "Svalbard & Jan Mayen","Vatican City"]

EXTRAS = {
'eng' => 'England',
'wal' => 'Wales',
'sco' => 'Scotland',
'nlr' => 'Northern Ireland'
}

def initialize(name, code)
@name = name
@code = code
end

def deserialize(object)
return nil unless object
Country.new(object['name'], object['code'])
end

def serialize(country)
{:name => country.name, :code => country.code}
end

def self.all
add_extras(filter(TZInfo::Country.all.map{|country| to_country country})).sort! {|c1, c2| c1.name <=> c2.name}
end

def self.get(code)
begin
to_country TZInfo::Country.get(code)
rescue TZInfo::InvalidCountryCode => e
'InvalidCountryCode' unless EXTRAS.has_key? code
Country.new EXTRAS[code], code
end
end

def self.get_by_name(name)
all.select {|country| country.name.downcase == name.downcase}.first
end

def self.filter(countries)
countries.reject {|country| FILTERS.include?(country.name)}
end

def self.add_extras(countries)
countries + EXTRAS.map{|k,v| Country.new v, k}
end

private
def self.to_country(country)
Country.new country.name, country.code
end
end

以及我对从另一个类调用的数组的请求

  def countries_ive_drunk
(had_drinks.map {|drink| drink.beer.country }).uniq
end

如果我抛出数组,我可以看到结构是:

[
#<Country:0x5e3b4c8 @name="Belarus", @code="BY">,
#<Country:0x5e396e0 @name="Britain (UK)", @code="GB">,
#<Country:0x5e3f350 @name="Czech Republic", @code="CZ">,
#<Country:0x5e3d730 @name="Germany", @code="DE">,
#<Country:0x5e43778 @name="United States", @code="US">,
#<Country:0x5e42398 @name="England", @code="eng">,
#<Country:0x5e40f70 @name="Aaland Islands", @code="AX">,
#<Country:0x5e47978 @name="England", @code="eng">,
#<Country:0x5e46358 @name="Portugal", @code="PT">,
#<Country:0x5e44d38 @name="Georgia", @code="GE">,
#<Country:0x5e4b668 @name="Germany", @code="DE">,
#<Country:0x5e4a2a0 @name="Anguilla", @code="AI">,
#<Country:0x5e48c98 @name="Anguilla", @code="AI">
]

这个是一样的,不管我做不做.uniq你都能看到有两个“Anguilla”

最佳答案

正如其他人所指出的,问题是 uniq 使用 hash 来区分国家,默认情况下,Object#hash 是对所有对象都不同。它还将使用 eql? 以防两个对象返回相同的 hash 值,以确定它们是否是 eql。

最好的解决办法是首先让你的类(class)正确!

class Country
# ... your previous code, plus:

include Comparable

def <=>(other)
return nil unless other.is_a?(Country)
(code <=> other.code).nonzero? || (name <=> other.name)
# or less fancy:
# [code, name] <=> [other.code, other.name]
end

def hash
[name, code].hash
end

alias eql? ==
end

Country.new("Canada", "CA").eql?(Country.new("Canada", "CA")) # => true

现在您可以对国家/地区数组进行排序,使用国家/地区作为散列键,比较它们等...

我包含了上面的代码来展示它通常是如何完成的,但在你的情况下,如果你继承 Struct(:code, :name)...

class Country < Stuct(:name, :code)
# ... the rest of your code, without the `attr_accessible` nor the `initialize`
# as Struct provides these and `hash`, `eql?`, `==`, ...
end

关于ruby - ruby 数组的 uniq 无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8778354/

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