我正在创建一个hash对象,以便编写一个小文件,一次读取一行文件,并将数组分配给我的哈希类。我得到了截然不同的结果,这取决于我是否将Hash子类化,加上使用我不理解的超级更改。
我的主要问题是没有子类化哈希(< Hash)它完美地工作,但是我没有Hash的方法(比如迭代键并从中获取东西......子类化Hash让我做那些事情,但似乎只存储散列数组的 last 元素....所以任何洞察你如何获得子类的方法。字典类是我发现的一个很好的例子在这个网站上,并完全按照我的意愿行事,所以我正在努力了解如何正确使用它。
filename = 'inputfile.txt.'
# ??? class Dictionary < Hash
class Dictionary
def initialize()
@data = Hash.new { |hash, key| hash[key] = [] }
end
def [](key)
@data[key]
end
def []=(key,words)
@data[key] += [words].flatten
@data[key]
# super(key,words)
end
end
listData = Dictionary.new
File.open(filename, 'r').each_line do |line|
line = line.strip.split(/[^[:alpha:]|@|\.]/)
puts "LIST-> #{line[0]} SUB-> #{line[1]} "
listData[line[0]] = ("#{line[1]}")
end
puts '====================================='
puts listData.inspect
puts '====================================='
print listData.reduce('') {|s, (k, v)|
s << "The key is #{k} and the value is #{v}.\n"
}
如果有人理解这里的子类化哈希值,并且有一些指针,那就太棒了。
无明确地运行&lt;散列:
./list.rb:34:in `<main>': undefined method `reduce' for #<Dictionary:0x007fcf0a8879e0> (NoMethodError)
这是我在尝试以任何方式迭代哈希时看到的典型错误。
以下是输入文件示例:
listA billg@microsoft.com
listA ed@apple.com
listA frank@lotus.com
listB evanwhite@go.com
listB joespink@go.com
listB fredgrey@stop.com
答案 0 :(得分:4)
我无法使用您的代码重现您的问题:
d = Dictionary.new #=> #<Dictionary:0x007f903a1adef8 @data={}>
d[4] << 5 #=> [5]
d[5] << 6 #=> [6]
d #=> #<Dictionary:0x007f903a1adef8 @data={4=>[5], 5=>[6]}>
d.instance_variable_get(:@data) #=> {4=>[5], 5=>[6]}
但是,如果你没有子类或包含定义它的类/模块,或者你自己定义它,你当然不会得reduce
。
您实施Dictionary
的方式肯定会有问题。您应该拨打super
,而不是尽可能重新实现。例如,只需这样就可以了:
class Dictionary < Hash
def initialize
super { |hash, key| hash[key] = [] }
end
end
d = Dictionary.new #=> {}
d['answer'] << 42 #=> [42]
d['pi'] << 3.14 #=> [3.14
d #=> {"answer"=>[42], "pi"=>[3.14]}
如果要重新实现内部哈希的存储方式和位置(即使用@data
),则必须重新实现至少each
(因为这几乎是所有Enumerable方法所调用的) to)和getters / setters。如果你只能改变一种方法,那就不值得了。
答案 1 :(得分:2)
Andrew Marshall's answer 已经正确了,您也可以在下面尝试这个替代方案。
从您的代码开始,我们可以假设您要创建一个对象 表现得像哈希,但行为有点不同。因此我们的第一个 代码将是这样的。
class Dictionary < Hash
为字典中的某个键指定新值将以不同方式完成 在这里。从上面的例子中,作业不会取代之前的作业 使用新值的值,而是将新值推送到上一个或更新 如果密钥尚未存在,则使用新值初始化的新数组。
这里我使用<<
运算符作为Array的push方法的简写。
此外,该方法返回值,因为它是超级的(参见if部分)
def []=(key, value)
if self[key]
self[key] << value
return value # here we mimic what super do
else
super(key, [value])
end
end
使用我们自己的类的优点是我们可以在类中添加新方法 并且所有实例都可以访问它。因此我们不需要 monkeypatch哈希类认为危险的事情。
def size_of(key)
return self[key].size if self[key]
return 0 # the case for non existing key
end
现在,如果我们将上述所有内容结合起来,我们将获得此代码
class Dictionary < Hash
def []=(key, value)
if self[key]
self[key] << value
return value
else
super(key, [value])
end
end
def size_of(key)
return self[key].size if self[key]
return 0 # the case for non existing key
end
end
player_emails = Dictionary.new
player_emails["SAO"] = "kirito@sao.com" # note no << operator needed here
player_emails["ALO"] = "lyfa@alo.com"
player_emails["SAO"] = "lizbeth@sao.com"
player_emails["SAO"] = "asuna@sao.com"
player_emails.size_of("SAO") #=> 3
player_emails.size_of("ALO") #=> 1
player_emails.size_of("GGO") #=> 0
p listData
#=> {"SAO" => ["kirito@sao.com", "lizbeth@sao.com", "asuna@sao.com"],
#=> "ALO" => ["lyfa@alo.com"] }
但是,当然,类定义可以用这一行代替
player_emails = Hash.new { [] }
# note that we wont use
#
# player_emails[key] = value
#
# instead
#
# player_emails[key] << value
#
# Oh, if you consider the comment,
# it will no longer considered a single line
答案完成后,我想评论一些示例代码:
filename = 'inputfile.txt.'
# Maybe it's better to use ARGF instead,
# so you could supply the filename in the command line
# and, is the filename ended with a dot? O.o;
File.open(filename, 'r').each_line do |line|
# This line open the file anonimously,
# then access each line of the file.
# Please correct me, Is the file will properly closed? I doubt no.
# Saver version:
File.open(filename, 'r') do |file|
file.each_line do |line|
# ...
end
end # the file will closed when we reach here
# ARGF version:
ARGF.each_line do |line|
# ...
end
# Inside the each_line block
line = line.strip.split(/[^[:alpha:]|@|\.]/)
# I don't know what do you mean by that line,
# but using that regex will result
#
# ["listA", "", "", "billg@microsoft.com"]
#
# Hence, your example will fail since
# line[0] == "listA" and line[1] == ""
# also note that your regex mean
#
# any character except:
# letters, '|', '@', '|', '\.'
#
# If you want to split over one or more
# whitespace characters use \s+ instead.
# Hence we could replace it with:
line = line.strip.split(/\s+/)
puts "LIST-> #{line[0]} SUB-> #{line[1]} "
# OK, Is this supposed to debug the line?
# Tips: the simplest way to debug is:
#
# p line
#
# that's all,
listData[line[0]] = ("#{line[1]}")
# why? using (), then "", then #{}
# I suggest:
listData[line[0]] = line[1]
# But to make more simple, actually you could do this instead
key, value = line.strip.split(/\s+/)
listData[key] = value
# Outside the block:
puts '====================================='
# OK, that's too loooooooooong...
puts '=' * 30
# or better assign it to a variable since you use it twice
a = '=' * 30
puts a
p listData # better way to debug
puts a
# next:
print listData.reduce('') { |s, (k, v)|
s << "The key is #{k} and the value is #{v}.\n"
}
# why using reduce?
# for debugging you could use `p listData` instead.
# but since you are printing it, why not iterate for
# each element then print each of that.
listData.each do |k, v|
puts "The key is #{k} and the value is #{v}."
end
好的,抱歉这么多,希望有所帮助。