0
나는 사용자 입력 변수 해시에 액세스하려고이 오류 메시지가 얻을액세스 해시 키, NoMethodError
이 설명해야 내 문제가 조금 더 좋다 ... 문자열 "n"이 변수에 저장되면 변수는 해시에 더 이상 액세스 할 수 없으며 nil을 반환합니다. 왜?
[6] pry(main)> line = "n"
=> "n"
[7] pry(main)> subway[:line]
=> nil
[8] pry(main)> subway[:"n"]
=> ["timessquare", "34th", "28th", "23rd", "unionsquare", "8th"]
[9] pry(main)> line
=> "n"
This is the .rb file
n = ["timessquare", "34th","28th","23rd","unionsquare","8th"]
l = ["8th", "6th", "unionsquare","3rd","1st"]
six = ["grandcentral","33rd","28th","23rd","unionsquare","astorplace"]
subway = {:n=>n, :l=> l, :six=>six}
puts "Where would you like to go?"
destination = gets.strip.chomp.downcase.delete(" ")
puts "What line is that on?"
endline = gets.strip.chomp.downcase.delete(" ")
puts "What station will you be departing from?"
location = gets.strip.chomp.downcase.delete(" ")
puts "What line is that on?"
startline = gets.strip.chomp.downcase.delete(" ")
if startline == endline
puts (startline.index(destination)-startline.index(location)).abs
else
puts "You have #{(subway[:startline].index(location) - subway[:startline].index("unionsquare")).abs} stops until transfer"
puts "After you have #{(subway[:endline].index(destination) - subway[:endline]index("unionsquare")).abs} until your destination"
end
단순히 심볼에 라인을 변환 할 필요가