2016-10-04 77 views
0

我收到說RB此錯誤消息:10:在「活動」:主未定義的局部變量或方法「beach_bum」:對象未定義的局部變量或方法主要:對象<NameError>爲Ruby

我通過輸入活動來調用方法/函數,我不確定我在這裏錯過了什麼?我的代碼列在下面。

def activities 
    puts " Hi, groupFish friends, you are in Beautiful San Diego Today." 
    puts "Do you want to check out La Jolla Beach swipe left or check out Coachella swipe right?" 
    puts "There are 25 things that are happening within 5 miles!" 

    print ">" 
    choice = $stdin.gets.chomp 

    if choice == "left" 
    beach_bum 
    elsif choice == "right" 
    coachella 
    else 
    puts "Keep on swiping and be sure use the search hashtag feature to find activities!" 
    end 
end 

活動

def beach_bum 
    puts " Tera and John are here watching the seals, care to join?" 
    puts " Do you want to meet them? Y or N." 
    choice= $stdin.gets.chomp 

    if choice == "Y" 
    puts "Great, we'll see you at the cove in 15 minutes." 
    exit(0) 
    elsif choice == "N" 
    puts " Keep swiping, there's rock climbing nearby" 
    else puts " Jamba Juice is having a free smoothie before 2pm, it's 2miles away" 
    end 
end 


def coachella 
    puts " Heck Yeah, you are about to enter one of the biggest concerts of all time" 
    puts " There are a total of 10,000 party goers today" 

end 

回答

1

您使用的方法beach_bum你定義它。 您只能在定義它之後使用一種方法。 我知道在某些語言中,這並不重要,但在Ruby中它確實。 您也可以在之後重新定義現有方法。

def foo; 'bar' ; end 
foo # bar 
def foo; 'changed' ; end 
foo # changed 

將您的代碼中的方法位置移動到活動方法之前。

+0

非常感謝Peter。這工作,並且我假設當我輸入coachella時不適合我。一旦我切換它的工作!另外,我不確定我是否理解這個邏輯,爲什麼命令很重要?我首先調用了活動函數,以便按照該順序運行。 –

+0

訂單很重要,因爲Ruby無法在未來進行時間旅行。如果方法在你調用的時候沒有定義(即使不存在),那麼Ruby應該如何執行它不知道它是什麼以及哪些不存在的東西? –

+0

@JörgW Mittag ThnxJörg,我在回答中已經說得更清楚了 Nathan,你能接受答案嗎?將來,當您發佈代碼時,請使用{}按鈕對其進行正確格式化 – peter

相關問題