2

我正在创建一个哈希对象,以便编写一个小脚本,一次读取文件一行,并将数组分配给我的哈希类。我得到的结果完全不同,这取决于我是否对 Hash 进行子类化,以及使用 super 更改我不理解的东西。

我的主要问题是,如果没有子类化哈希(< Hash)它可以完美地工作,但是我没有得到哈希的方法(比如迭代键并从中取出东西......子类化哈希让我做这些事情,但是它似乎只存储了散列数组的最后一个元素....所以任何关于如何获取子类方法的见解。Dictionary 类是我在这个网站上找到的一个很好的例子,并且完全符合我的要求,所以我试图了解如何正确使用它。

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"
}

如果有人了解这里发生的事情,继承哈希,并有一些指针,那就太好了。

在没有明确 < 哈希的情况下运行:

./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
4

2 回答 2

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 方法调用的)和 getter/setter。当您只能更改一种方法时,不值得付出努力。

于 2012-10-28T13:47:57.883 回答
2

虽然Andrew Marshall 的回答 已经正确,但您也可以在下面尝试这种替代方法。

从您的代码开始,我们可以假设您想要创建一个行为类似于 Hash 的对象,但行为略有不同。因此我们的第一个代码将是这样的。

class Dictionary < Hash

在这里为字典中的某个键分配一个新值会有所不同。从上面的示例中,分配不会用新值替换先前的值,而是将新值推送到先前的值或如果键尚不存在则使用新值初始化的新数组。

这里我使用<<操作符作为 Array 的 push 方法的简写。此外,该方法返回值,因为它是 super 所做的(参见 if 部分)

  def []=(key, value)
    if self[key]
      self[key] << value
      return value # here we mimic what super do
    else
      super(key, [value])
    end
  end

使用我们自己的类的优点是我们可以向该类添加新方法,并且所有实例都可以访问它。因此,我们不需要对被认为是危险事物的 Hash 类进行猴子补丁。

  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

好的,不好意思说了这么多,希望对你有帮助。

于 2012-10-28T13:49:12.843 回答