私はこのコードをRubyのCS割り当てに書いています。開こうとしているテキストファイルがプログラムで見つからないのはなぜですか?
V:\CS 300 RubyAssignment\lib\rubyAssignment.rb:13:in `categorize': undefined method `line' for #<File:ruby1.txt (closed)> (NoMethodError)
from V:\CS 300 RubyAssignment\lib\rubyAssignment.rb:11:in `open'
from V:\CS 300 RubyAssignment\lib\rubyAssignment.rb:11:in `categorize'
from V:\CS 300 RubyAssignment\lib\rubyAssignment.rb:30
コードは以下の通りですが、私は感じて私のテキストファイルであり得る:私はちょうどので、私はそれについて多くを知らないが、私はこのように、このコードから何の方法のエラーを取得していない続けるRubyで始めています間違った場所。私はNetBeans Rubyプラグインを使用しています。テキストファイルがプロジェクトのソースファイルフォルダ、テストファイルフォルダ、またはネットビーンのライブラリフォルダになければならないのかどうかわかりません。それはどんなアイデアと同じくらい簡単かもしれませんか?
# This program reads a file line by line,
# separating lines by writing into certain text files.
# PPQ - Pangrams, Palindromes, and Quotes
class PPQ
def categorize
file_pangram = File.new('pangram.txt', 'w')
file_palindrome = File.new('palindrome.txt', 'w')
file_quotes = File.new('quotes.txt','w')
File.open('ruby1.txt','r') do |file|
while line = file.gets
if(file.line.reverse == file.line)
file_palindrome.write line
if(file.line.contains('a'&&'b'&&'c'&&'d'&&'e'&&'f'&&'g'&&'h'&&'i'&&'j'&&'k'&&'l'&&'m'&&'n'&&'o'&&'p'&&'q'&&'r'&&'s'&&'t'&&'u'&&'v'&&'w'&&'x'&&'y'&&'z'))
file_pangram.write "file.line"
else
file_quotes.write "file.line"
end
end
end
file.close
file_pangram.close
file_palindrome.close
file_quotes.close
end
end
end
my_ruby_assignment = PPQ.new
my_ruby_assignment.categorize
なぜこれはあなたのファイルを見つけることはないと思いますか?実際にエラーメッセージを読んだことはありますか? – geekosaur
基本的に私の質問をよりシンプルにするために、私のテキストファイルはどこにあるのでしょうか?netbeansはこのコードを実行できますか? ruby1.txtのディレクトリを指定するか、ruby1.txtを呼び出すだけですか? –
それはファイルの場所とは関係ありません。私の答えを参照してください... – andrewdotnich