feat: add write_txt method to Student class

pull/4/head lab2.2
Artem-Darius Weber 4 months ago
parent acf69c4385
commit dc752cdf43

@ -49,7 +49,7 @@ class Student < Person
File.foreach(file_path) do |line|
line.strip!
next if line.empty? # Пропустить пустые строки
next if line.empty?
begin
student = from_string(line)
@ -61,6 +61,20 @@ class Student < Person
students
end
def self.write_to_txt(file_path, students)
raise ArgumentError, "Expected an array of Student objects" unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
File.open(file_path, 'w') do |file|
students.each do |student|
file.puts student.to_s
end
end
puts "Data successfully written to #{file_path}"
rescue IOError => e
puts "An error occurred while writing to the file: #{e.message}"
end
def surname_and_initials
"#{@surname} #{name[0]}.#{patronymic[0]}."

Loading…
Cancel
Save