[英]Passing a filename as an argument in ruby
def file_to_array(filename)
my_text = []
File.open(filename, "r").each do |line|
my_text << line
end
my_text
end
你好! 我的方法應該在調用時接收文件名,打開文件並將每一行移動到數組。 但是,它在調用時會引發undefined local variable
,並且我假定它與filename [DOT] ext?!有關。 我的通話發生在以下方法中
def clean_array
cleaned = file_to_array(text.txt).map do |element|
element.gsub(/\b['s\!\.\\n]*/m, "")
end
cleaned.map! do |el|
el.split(" ")
end
return cleaned.flatten!
#p cleaned
end
文件名應為字符串,並以引號引起來:
file_to_array("text.txt")
# OR
file_to_array('text.txt')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.