-1
私はすべての連絡先を検索し、連絡先を削除し、連絡先を追加するこの電話帳プログラムを持っています。 contactList
という名前のハッシュグローバル変数を作成しました。しかし、プログラムはこれを認識できません。私は何を間違えたのですか?私のハッシュがRubyで動作しないのはなぜですか?
class PhoneBook
contactList = hash.new
def Add(newContact = {})
flag = false
if newContact.length < 1
return flag
else
flag = true
newContact.collect do |name, number|
contactList[name] = number
end
return flag
end
end
def delete (targetName)
if !contactList.has_key?(targetName)
return false
else
contactList.delete(targetName)
return true
end
end
def displayContact (targetName)
number = -1
if contactList.has_key?(targetName)
number = contactList(targetName)
puts "Contact name : #{targetName}, Contact Number, #{number}"
else
puts "#{targetName} doesn't exist in the phonebook"
end
end
def displayAllContacts
if !contactList.empty?
contactList.each {|name, number| puts "Contact name: #{name}, contact number #{number}" }
else
puts "You don't have any contact details your phonebook"
end
end
end
入手しているエラーメッセージを追加できますか? IMHO問題は、新しいハッシュを作成するために不正な構文を使用していることです。 'hash.new'の代わりに' Hash.new'を使用してください。 –
私はあなたが正しいと信じています。これは未定義のメソッドですが、contactListがローカル変数として宣言されているようです。 – Codes316