내가 식사 플래너 프로그램을 만드는 오전 그것은 나에게 오류를주고 유지는 :루비 정의되지 않은 방법`[] = '1 : Fixnum이라는
main.rb:20:in `update': undefined method `[]=' for 1:Fixnum (NoMethodError)
이 코드입니다 :
print ("Welcome to the meal planner program")
def update(aMeal,aCal)
print"would u like to change drink or entree"
userWant = gets.chomp
if userWant =="entree"
print("Enter your new meal")
newMeal = gets.chomp
aMeal [0][0] = newMeal
print("Enter the cals")
newCal = gets.chomp
aCal[0][1] = newCal
end
if userWant == "drink"
print("enter your new drink")
newDrink = gets.chomp
aMeal[1][0] = newDrink
print("Enter the cals :")
newCal = gets.chomp
aCal[1][1] = newCal
end
end
def main
mondayMeal= [["Fettucine Alfredo",300],[" Drink of Choice!",150]]
cals = mondayMeal[0][1] + mondayMeal[1][1]
done = false
while done == false do
puts ("Do you want to 1.view your meals 2.modify your meals")
userChoice = gets.chomp
if userChoice == "1"
print "Your meal is" , mondayMeal[0][0] , "and" , mondayMeal[1][0]
print "your calories are" , mondayMeal[0][1] + mondayMeal[1][1]
end
if userChoice == "2"
print "what day?"
newDay = gets.chomp
if newDay == "monday"
update(mondayMeal,cals)
end
end
end
end
main
@lurker :'[] '를 사용하여'Integer'로 인덱싱하는 것은 괜찮습니다. 2의 보수 바이너리 표현의 최하위 비트를 리턴 할 것입니다. * Setting *은'[] = '를 사용하는 것이 문제입니다. –
Ruby 코딩 팁으로 코드를 들여 쓰는 데 2 개의 공백을 사용하고 들여 쓰기를 유지하는 데 항문이됩니다. 프로그래밍 실습을 훨씬 쉽게 할 수있는 코딩 연습입니다. 또한 Ruby에서는 camelCase가 아니라 snake_case를 변수 이름으로 사용합니다. CamelCaseIsTooDifficultToRead_snake_case_is_easier. –
@ JörgWMittag 감사합니다. 나는 그 사실을 몰랐습니다. – lurker