Ruby未定义方法,但方法存在

问题描述 投票:1回答:2

我对ruby还是很陌生,我正在创建一个CLI应用程序,但是red_meat出现了未定义的方法错误,我快要生气了。如果已经定义了该方法,或者我认为该方法,我将如何定义它?

def menu
    puts "Are you interested in finding some delicious recipes?"
    puts ""
    puts "Enter yes or no"
    case (gets.chomp.downcase)
    when 'no'
      puts "Aww you missed out on some yummy meals. Thank you for dropping by"
    when 'yes'
      puts "Great! Enter one of the following options (Red meat, Poultry, Seafood or Vegetarian)"
      #case (gets.chomp.downcase)
      input = gets.strip.downcase
      if input = "red meat"
        red_meat
      end
    else
      puts "I'm sorry, are you hangry? Please try again"
      puts ""
      menu
    end

    def red_meat
      puts "red meat recipe 1"
      puts "red meat recipe 2"
      puts "red meat recipe 3"
      puts "red meat recipe 4"
    end 

错误

ERROR: 
cli.rb:22:in `menu': undefined method `red_meat' for #<RecipesCli::CLI:0x00007fd941914228> (NoMethodError)
ruby command-line-interface
2个回答
0
投票

您需要先定义您的方法,以后才能调用它。 menu方法的内部或外部。


-1
投票

如从代码的缩进中看到的,read_meat的方法定义嵌套在menu的方法定义的inside

© www.soinside.com 2019 - 2024. All rights reserved.