Ruby/File Directory/each

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

each word reading

# Create a file...
open("end_separated_records", "w") do |f|
  f << %{This is record one.
It spans multiple lines.ENDThis is record two.END}
end
# And read it back in.
open("end_separated_records") { |f| f.each("END") { |record| p record } }
# "This is record one.\nIt spans multiple lines.END"
# "This is record two.END"



Read line by line with each

open("newline_separated_records", "w") do |f|
  f.puts "This is record one. It cannot span multiple lines."
  f.puts "This is record two."
end
open("newline_separated_records") { |f| f.each { |x| p x } }
# "This is record one. It cannot span multiple lines.\n"
# "This is record two.\n"