大小读取和写入文件和条件语句
Size Reading and Writing to File and conditional statements
初学Ruby。正在做一个读写文件的练习。
name_number = {
}
File.open("book.txt", 'a') do |file|
name_number.each do |name, number|
file.write ("- #{name} #{number}\n")
end
file.close
end
File.open("book.txt", "r") do |file|
if file.size < 1
puts "There are no people in the book."
end
File.open("book.txt", "r") do |file|
puts file.read
end
end
所以输出“书中没有人”。当文件为空时。
所以如果我们添加一些人...
name_number = {
"Bill" => 87987,
"Kevin" => 78912
}
File.open("book.txt", 'a') do |file|
name_number.each do |name, number|
file.write ("- #{name} #{number}\n")
end
file.close
end
我在想怎么让它说
“有一个人”为1
和“有些人”表示任何超过 1 的东西。
我想我需要一个像这样的 elsif
声明,(但显然这行不通,但也许你可以看到我想要实现的目标?)。
File.open("book.txt", "r") do |file|
if file.size < 1
puts "There are no people in the book."
elsif file.size == 1
puts "There is an entry in the book."
elsif file.size == 2
puts "There are two entries in the book"
end
File.open("book.txt", "r") do |file|
puts file.read
end
end
我肯定漏掉了什么。我想知道是否有人可以指出正确的方向?
file.size 给出字节数。你可能想要 file.readlines.size
我发现如果我将 .count 与 readlines 一起使用,我就能解决我的问题!
更新代码
File.open("book.txt", "r") do |file|
number = file.readlines.count
if number < 1
puts "There are no people in the book."
elsif number == 1
puts "There is one person in the book"
File.open("book.txt", "r") do |file|
puts file.read
end
elsif number == 2
puts "There are two people in the book."
File.open("book.txt", "r") do |file|
puts file.read
end
elsif number >= 3
puts "There are some entries in the book."
File.open("book.txt", "r") do |file|
puts file.read
end
end
end
要获得您需要阅读的文件的行数。如果可能,您希望避免多次读取文件。
假设文件不是太大,您可以 gulp 它 成一个数组,使用 IO::readlines:
arr = File.readlines("book.txt")
puts case arr.size
when 0
"There are no people in the book."
when 1
"There is one person in the book"
when 2
"There are two people in the book"
else
"There are some entries in the book"
end
puts arr
或者,您可以 gulp it 到一个字符串中,使用 IO::read:
str = File.read("book.txt")
puts case str.count("\n")
when 0
"There are no people in the book."
when 1
"There is one person in the book"
when 2
"There are two people in the book"
else
"There are some entries in the book"
end
puts str
如果文件太大,您需要一次一行地阅读它,您可以使用IO::foreach。但是,这确实需要两次遍历文件。
puts case File.foreach("book.txt").count
when 0
"There are no people in the book."
when 1
"There is one person in the book"
when 2
"There are two people in the book"
else
"There are some entries in the book"
end
File.foreach("book.txt") { |line| puts line }
初学Ruby。正在做一个读写文件的练习。
name_number = {
}
File.open("book.txt", 'a') do |file|
name_number.each do |name, number|
file.write ("- #{name} #{number}\n")
end
file.close
end
File.open("book.txt", "r") do |file|
if file.size < 1
puts "There are no people in the book."
end
File.open("book.txt", "r") do |file|
puts file.read
end
end
所以输出“书中没有人”。当文件为空时。
所以如果我们添加一些人...
name_number = {
"Bill" => 87987,
"Kevin" => 78912
}
File.open("book.txt", 'a') do |file|
name_number.each do |name, number|
file.write ("- #{name} #{number}\n")
end
file.close
end
我在想怎么让它说
“有一个人”为1 和“有些人”表示任何超过 1 的东西。
我想我需要一个像这样的 elsif
声明,(但显然这行不通,但也许你可以看到我想要实现的目标?)。
File.open("book.txt", "r") do |file|
if file.size < 1
puts "There are no people in the book."
elsif file.size == 1
puts "There is an entry in the book."
elsif file.size == 2
puts "There are two entries in the book"
end
File.open("book.txt", "r") do |file|
puts file.read
end
end
我肯定漏掉了什么。我想知道是否有人可以指出正确的方向?
file.size 给出字节数。你可能想要 file.readlines.size
我发现如果我将 .count 与 readlines 一起使用,我就能解决我的问题!
更新代码
File.open("book.txt", "r") do |file|
number = file.readlines.count
if number < 1
puts "There are no people in the book."
elsif number == 1
puts "There is one person in the book"
File.open("book.txt", "r") do |file|
puts file.read
end
elsif number == 2
puts "There are two people in the book."
File.open("book.txt", "r") do |file|
puts file.read
end
elsif number >= 3
puts "There are some entries in the book."
File.open("book.txt", "r") do |file|
puts file.read
end
end
end
要获得您需要阅读的文件的行数。如果可能,您希望避免多次读取文件。
假设文件不是太大,您可以 gulp 它 成一个数组,使用 IO::readlines:
arr = File.readlines("book.txt")
puts case arr.size
when 0
"There are no people in the book."
when 1
"There is one person in the book"
when 2
"There are two people in the book"
else
"There are some entries in the book"
end
puts arr
或者,您可以 gulp it 到一个字符串中,使用 IO::read:
str = File.read("book.txt")
puts case str.count("\n")
when 0
"There are no people in the book."
when 1
"There is one person in the book"
when 2
"There are two people in the book"
else
"There are some entries in the book"
end
puts str
如果文件太大,您需要一次一行地阅读它,您可以使用IO::foreach。但是,这确实需要两次遍历文件。
puts case File.foreach("book.txt").count
when 0
"There are no people in the book."
when 1
"There is one person in the book"
when 2
"There are two people in the book"
else
"There are some entries in the book"
end
File.foreach("book.txt") { |line| puts line }