未定义的局部变量或main方法:Object <nameerror>对于Ruby

时间:2016-10-04 09:08:03

标签: ruby

我收到的错误消息是rb:10:在'activities'中:未定义的局部变量或方法'beach_bum'for main:Object

我通过输入活动来调用方法/函数,我不确定我在这里缺少什么?我的代码如下所示。

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 个答案:

答案 0 :(得分:1)

在定义之前使用了方法beach_bum。 您只能在定义方法后使用方法。 我知道在某些语言中这没关系,但在Ruby中它确实如此。 您也可以通过以后重新定义来修改现有方法。

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

将代码中方法的位置移动到活动方法之前。