0

I'm getting this error message that says rb:10:in 'activities': undefined local variable or method 'beach_bum' for main:Object

I'm calling the method/function by typing in activities, I'm not sure what I'm missing here? My code is listed below.

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

activities

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 Answer 1

1

You used the method beach_bum before you defined it. You can only use a method after you defined it. I know that in some languages this doesn't matter but in Ruby it does. You can also modify an existing method by redefining it afterward.

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

Move the location of the method in your code to before the activities method.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much Peter. This worked, and I'm assuming when I typed in coachella that didn't work for me. Once I switched it around it worked! Also, I'm not sure if I understand this logic, why does the order matter? I called out the activities function first so it would run in that order.
The order matters because Ruby cannot time-travel into the future. If the method is not defined (i.e. does not even exist) when you call it, how is Ruby supposed to execute something it doesn't know what it is and which doesn't even exist?
@Jörg W Mittag Thnx Jörg, I've made this more clear in my answer Nathan, Could you accept the answer ? In the future when you publish code, please use the {} button to correctly format it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.