#ruby
#ruby
Вопрос:
Я исхожу из фона python и не могу понять, почему я получаю сообщение об ошибке. Я не могу найти, где я не заканчиваю ни один из своих циклов. Это работает, если у меня нет всего в функциях.
namelist = ["eric","lena","austin","booger"]
counter = 0
names = Hash.new
def start()
puts "enter name"
print "> "
input = $stdin.gets.chomp
namelist.push(input)
more_names()
hash_portion()
end
def more_names()
puts "press 1 to add another name otherwise press 2 if you're done"
print "> "
choice = $stdin.gets.chomp
if choice == 1
puts "> "
input1 = $stdin.gets.chomp
namelist.push(input1)
more_names()
elsif choice == 2
nil
else
more_names()
end
end
def hash_portion()
until counter == namelist.length do
n_namelist = namelist.sample(1000000000000)
n_namelist.each do |n|
counter = 1
names ["#{counter}"] = "#{n}"
end
end
names.each do |digit,person|
puts "#{digit}. #{person}"
end
end
start()
Ответ №1:
Удалите пробел в этой строке:
names ["#{counter}"] = "#{n}"
Должно выглядеть так:
names["#{counter}"] = "#{n}"
Комментарии:
1. Это сработало. Большое вам спасибо! Я весь день ломал голову над этим.