diff --git a/lab2/student.rb b/lab2/student.rb index 6d2dd9e..57d26a7 100644 --- a/lab2/student.rb +++ b/lab2/student.rb @@ -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]}."