J
Joey Zhou
# ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
get_page_hash = {}
get_page_hash.default = []
File.foreach("page.txt") do |line|
word, page = line.chomp.split(':')
get_page_hash[word] << page # the problem is here
end
p get_page_hash['Aword'] # => ["1", "2", "3", "4", "5"]
p get_page_hash['Bword'] # => ["1", "2", "3", "4", "5"]
p get_page_hash.default # => ["1", "2", "3", "4", "5"]
__END__
content of page.txt:
Aword:1
Bword:2
Cword:3
Aword:4
Dword:5
Simple program, clear purpose. I don't know why get_page_hash.default
becomes ["1", "2", "3", "4", "5"], it seems radiculous.
Only if I modify the very line to:
get_page_hash[word] += [page]
I get what I want:
p get_page_hash['Aword'] # => ["1", "4"]
p get_page_hash['Bword'] # => ["2"]
p get_page_hash.default # => []
I think use "<<" maybe intuitive, but the result is unexpected. What's
wrong with it?
Thank you!
Joey
get_page_hash = {}
get_page_hash.default = []
File.foreach("page.txt") do |line|
word, page = line.chomp.split(':')
get_page_hash[word] << page # the problem is here
end
p get_page_hash['Aword'] # => ["1", "2", "3", "4", "5"]
p get_page_hash['Bword'] # => ["1", "2", "3", "4", "5"]
p get_page_hash.default # => ["1", "2", "3", "4", "5"]
__END__
content of page.txt:
Aword:1
Bword:2
Cword:3
Aword:4
Dword:5
Simple program, clear purpose. I don't know why get_page_hash.default
becomes ["1", "2", "3", "4", "5"], it seems radiculous.
Only if I modify the very line to:
get_page_hash[word] += [page]
I get what I want:
p get_page_hash['Aword'] # => ["1", "4"]
p get_page_hash['Bword'] # => ["2"]
p get_page_hash.default # => []
I think use "<<" maybe intuitive, but the result is unexpected. What's
wrong with it?
Thank you!
Joey