我正在为 Ruby 中的 CS 作业编写此代码。我刚开始使用 Ruby,所以我不太了解它,但我不断从这段代码中得到一个 no method 错误,如下所示:
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
代码如下,但我感觉我的文本文件放错了地方。我使用 NetBeans Ruby 插件,但我不知道我的文本文件是否应该在 netbeans 的项目源文件夹、测试文件夹或库文件夹中?可能就这么简单,有什么想法吗?
# 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