2016-10-04 73 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

相关问题