我有兩個文件:紅寶石1.8.7未定義的局部變量
confidant.rb:
# 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
中的錯誤信息運行紅寶石confidant.rb結果:12:未定義的局部變量或方法'code_words'爲 main:Object(NameError)
我必須以某種方式限定code_words嗎?代碼是來自_why的尖銳指南的稍微改編的例子。
感謝這兩個答案。 – lowerkey
如果你要在最後換行,你應該使用'puts'而不是'print'。 –