Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 未定义的局部变量或方法';用户响应';_Ruby_Undefined - Fatal编程技术网

Ruby 未定义的局部变量或方法';用户响应';

Ruby 未定义的局部变量或方法';用户响应';,ruby,undefined,Ruby,Undefined,我已经上下阅读了大约30分钟的代码。我一辈子都看不到用户的响应是未定义的。我对编码非常陌生,所以我不知道有多少代码适合粘贴在这里。我认为启动和获取行动是必要的,但其他的不会有什么坏处吧 error => rb:32:in `launch!': undefined local variable or method `user_response' for <Guide:0x007fb019984718> (NameError) class Guide class Con

我已经上下阅读了大约30分钟的代码。我一辈子都看不到用户的响应是未定义的。我对编码非常陌生,所以我不知道有多少代码适合粘贴在这里。我认为启动和获取行动是必要的,但其他的不会有什么坏处吧

error => rb:32:in `launch!': undefined local variable or method `user_response' for
<Guide:0x007fb019984718> (NameError)

class Guide

    class Config
        @@actions = ['list', 'find', 'add', 'quit']
        def self.actions
        @@actions
        end
    end

    def initialize(path=nil)
        # locate the restaurant text file at path
        Restaurant.filepath = path
        if Restaurant.file_usable?
            puts "Found restaurant file."
        # or IF a new text file can't be created, create a new file
        elsif Restaurant.create_file
            puts "Created restaurant file."
        # exit if create fails
        else
            puts "Exiting"
            exit!
        end
    end

    def launch! #! means that it is a strong powerful method!
        introduction
        # action loop
        result = nil
        until result == :quit
            action = get_action 
            result = do_action(user_response)
        end
        conclusion
    end

    def get_action
        action = nil
        # Keep asking for user input until we get a valid action
        until Guide::Config.actions.include?(action)
        puts "Actions: " + Guide::Config.actions.join(", ") if action
        print "> "
        user_response = gets.chomp
        action = user_response.downcase.strip
        end
        return action
    end

    def do_action(action)
        case action 
        when 'list'
            puts "Listing..."
        when 'find'
            puts "Finding..."
        when 'add'
            puts "Adding..."
        when 'quit'
            return :quit
        else puts " I don't understand that command."
        end

    end  

    def introduction
        puts "<<< Welcome to the Food Finder >>>"
        puts "This is an interactive guide to help you find the food you crave."
    end

    def conclusion
        puts "<<< Goodbye and Bon Appetit! >>>>"
    end

end
error=>rb:32:in'launch!':未定义的局部变量或方法“user\u response”
(名称错误)
课堂指导
类配置
@@操作=['list'、'find'、'add'、'quit']
自我行动
@@行动
结束
结束
def初始化(路径=零)
#在路径中找到餐厅文本文件
Restaurant.filepath=path
如果Restaurant.file\u可用?
放上“找到餐厅档案”
#或者,如果无法创建新文本文件,请创建新文件
elsif Restaurant.create_文件
放入“已创建的餐厅文件”
#如果创建失败,请退出
其他的
“退出”
出口
结束
结束
def启动!#!意味着这是一个强大的方法!
介绍
#动作回路
结果=零
直到结果==:退出
动作=获取动作
结果=执行操作(用户响应)
结束
结论
结束
def get_行动
动作=零
#继续请求用户输入,直到我们得到有效的操作
直到指南::Config.actions.include?(操作)
将“Actions:+Guide::Config.Actions.join(“,”)放在操作中
打印“>”
用户\u响应=gets.chomp
action=user\u response.downcase.strip
结束
返回动作
结束
def do_动作(动作)
案例诉讼
当‘列表’
将“列表…”
什么时候“找到”
将“查找…”
当“添加”时
放入“添加…”
什么时候“退出”
返回:退出
else说“我不懂这个命令”
结束
结束
def简介
放置“>”
“这是一个互动指南,帮助你找到你渴望的食物。”
结束
定义结论
放置“>>”
结束
结束

我想您应该这样做:

def launch! #! means that it is a strong powerful method!
    introduction
    # action loop
    result = nil
    until result == :quit
        result = do_action(get_action)
    end
    conclusion
end

唯一一次定义名为
user\u response
的变量是在
get\u action
方法中。
您在那里定义它的方式使它成为一个局部变量,除了在
get\u action
方法中,它将无法从任何地方访问。

in
lauch方法,我看到这一行
result=do\u action(user\u response)
,但是
user\u response
之前没有定义,在这里声明
user\u response