【发布时间】:2011-10-30 00:55:53
【问题描述】:
我有两个文件:
# wordlist.rb
code_words = {
'computer' => 'devil worshipping device',
'penny' => 'pretty',
'decoration' => 'girlandes, green light bulbs, skeletons',
'pets' => 'captured souls'
}
和
# confidant.rb
require 'wordlist'
# Get string and swap in code words
print 'Say your piece:
'
idea = gets
code_words.each do |original, translated|
idea.gsub! original, translated
end
# Save the translated idea to a new file
print 'File encoded. Please enter a name for your piece:
'
piece_name = gets.strip
File::open 'piece-' + piece_name + '.txt', 'w' do |f|
f << idea
end
运行 ruby confidant.rb 会导致错误消息:
confidant.rb:12: 未定义的局部变量或方法'code_words' 主:对象(名称错误)
我必须以某种方式限定 code_words 吗?该代码是 _why's poignant guide 中的一个略微改编的示例。
【问题讨论】:
-
感谢两位的回答。
-
如果你要在末尾换行,你应该使用
puts而不是print。
标签: ruby