Andrew Marshallの答えはすでに正しいですが、以下の代替案を
試すこともできます.
あなたのコードから、ハッシュのように振る舞うオブジェクトを作成したいと思うかもしれませんが、動作は少し異なります。したがって、最初のコードは次のようになります。
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
独自のクラスを使用する利点は、クラスに新しいメソッドを追加できることであり、そのすべてのインスタンスからアクセスできるようになります。したがって、危険なことを考慮した 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"] }
しかし、確かに、クラス定義はこの1行で置き換えることができます
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
OK、おしゃべりしてすみません。お役に立てば幸いです。