ruby-on-rails – ‘Require’方法不起作用 – 帮忙?
发布时间:2020-12-17 01:59:54 所属栏目:百科 来源:网络整理
导读:我刚刚开始学习 Ruby,我一直在关注为什么(尖锐的)指南.有一次,我创建了一个名为’wordlist.rb’的文件,其中包含以下代码: code_words = { 'starmonkeys' = 'Phil and Pete,those prickly chancellors of the New Reich','catapult' = 'chucky go-go','fireb
我刚刚开始学习
Ruby,我一直在关注为什么(尖锐的)指南.有一次,我创建了一个名为’wordlist.rb’的文件,其中包含以下代码:
code_words = { 'starmonkeys' => 'Phil and Pete,those prickly chancellors of the New Reich','catapult' => 'chucky go-go','firebomb' => 'Heat-Assisted Living','Nigeria' => "Ny and Jerry's Dry Cleaning (with Donuts)",'Put the kabosh on' => 'Put the cable box on' } 另一个脚本调用wordlist文件中的’require’方法…. require 'wordlist' # Get evil idea and swap in code words print "Enter your new idea: " idea = gets code_words.each do |real,code| idea.gsub!( real,code ) end # Save the jibberish to a new file print "File encoded. Please enter a name for this idea: " idea_name = gets.strip File::open( "idea-" + idea_name + ".txt","w" ) do |f| f << idea end 现在,无论出于何种原因,当我尝试运行上面的脚本时出现此错误: test.rb:5:未定义的局部变量或方法`code_words’for main:Object(NameError) 它肯定找到并加载wordlist.rb文件(该方法返回true),但我似乎无法访问code_words哈希.知道是什么原因引起的吗? 解决方法
code_words是wordlist.rb中的局部变量.
你可以做什么: >定义一个gloibal变量($wordlist)>定义一个常量(WORDLIST)>定义另一个容器,例如提供数据的类 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |