Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在ruby中将文件的一行作为字符串插入数组_Arrays_Ruby_File_Io - Fatal编程技术网

Arrays 如何在ruby中将文件的一行作为字符串插入数组

Arrays 如何在ruby中将文件的一行作为字符串插入数组,arrays,ruby,file,io,Arrays,Ruby,File,Io,我创建了一个方法,允许你在一行中写一个问题,在紧接着的下一行中写它的答案,这样文件只有奇数行中的问题和偶数行中的问题的答案,我想将奇数行(问题)作为字符串添加到数组中,偶数行(答案)也作为字符串添加到另一个数组中,所以这个问题的答案,首先位于@questions,首先位于@answers 这是我的代码: def initialize @file = File.new("questionary.out", "a") @questions = [] @

我创建了一个方法,允许你在一行中写一个问题,在紧接着的下一行中写它的答案,这样文件只有奇数行中的问题和偶数行中的问题的答案,我想将奇数行(问题)作为字符串添加到数组中,偶数行(答案)也作为字符串添加到另一个数组中,所以这个问题的答案,首先位于@questions,首先位于@answers

这是我的代码:

def initialize
        @file = File.new("questionary.out", "a")
        @questions = []
        @answers = []
end

def append_to_file

    puts
    puts "PLEASE TYPE THE QUESTION THAT YOU WANT TO ADD"
    puts
    append_question = gets
    @file << append_question

    puts
    puts "PLEASE TYPE IT'S ANSWER"
    puts
    append_answer = gets
    @file << append_answer

    @file.close


    #INSERT INTO THE ARRAYS 
    i = 0
    File.open("questionary.out") do |line|
        i = i+1

        if i % 2 == 0
            @answers << line.to_s
        else 
            @questions << line.to_s
        end
    end
end
def初始化
@file=file.new(“questionary.out”、“a”)
@问题=[]
@答案=[]
结束
def将_附加到_文件
放
放置“请键入要添加的问题”
放
append\u question=get
@文件使用

使用


假设您有一个名为
foo.txt
的文件,其中问题和答案交替排列,您可以使用
IO::each_line
遍历该文件

您还可以使用神秘的
$。
全局变量(其中包含“上次读取的文件的当前输入行号”)和
Integer::odd?
方法来适当填充数组。例如:

questions = []
answers = []

File.open("foo.txt", "r+").each_line do |l|
  if $..odd?
    questions << l
  else
    answers << l
  end
end

# one-liner for kicks
# File.open("foo.txt", "r+").each_line { |l| $..odd? ? questions << l : answers << l }
问题=[]
答案=[]
打开(“foo.txt”,“r+”)。每行|
如果美元…奇数?

问题假设您有一个名为
foo.txt
的文件,其中问题和答案交替排列,您可以使用
IO::each_line
遍历该文件

您还可以使用神秘的
$。
全局变量(其中包含“上次读取的文件的当前输入行号”)和
Integer::odd?
方法来适当填充数组。例如:

questions = []
answers = []

File.open("foo.txt", "r+").each_line do |l|
  if $..odd?
    questions << l
  else
    answers << l
  end
end

# one-liner for kicks
# File.open("foo.txt", "r+").each_line { |l| $..odd? ? questions << l : answers << l }
问题=[]
答案=[]
打开(“foo.txt”,“r+”)。每行|
如果美元…奇数?

问题首先,让我们创建一个文件:

q_and_a =<<-END
Who was the first person to set foot on the moon?
Neil Armstrong
Who was the voice of the nearsighted Mr. Magoo?
Jim Backus
What was nickname for the Ford Model T?
Tin Lizzie
END

FName = "my_file"
File.write(FName, q_and_a)
  #=> 175

首先,让我们创建一个文件:

q_and_a =<<-END
Who was the first person to set foot on the moon?
Neil Armstrong
Who was the voice of the nearsighted Mr. Magoo?
Jim Backus
What was nickname for the Ford Model T?
Tin Lizzie
END

FName = "my_file"
File.write(FName, q_and_a)
  #=> 175