You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kubsu-sm5-ruby/lab2/providers/students_list_txt.rb

34 lines
748 B

require_relative '../student'
require_relative 'persistence_strategy'
class TXTPersistenceStrategy
include PersistenceStrategy
def load(filename)
if File.exist?(filename)
File.open(filename, 'r') do |file|
file.each_line.map do |line|
line.strip!
next if line.empty?
begin
Student.from_string(line)
rescue ArgumentError => e
warn "Ошибка при парсинге строки: #{line}. #{e.message}"
nil
end
end.compact
end
else
[]
end
end
def save(filename, students)
File.open(filename, 'w') do |file|
students.each do |student|
file.puts student.to_s
end
end
end
end