From acf69c43856353ad128bbd5c1ec92b503f536f35 Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Sat, 21 Sep 2024 19:22:37 +0300 Subject: [PATCH] feat: Add read_from_txt method to Student class - Introduced read_from_txt method in Student class to read student data from a text file. --- lab2/student.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lab2/student.rb b/lab2/student.rb index f0a5121..6d2dd9e 100644 --- a/lab2/student.rb +++ b/lab2/student.rb @@ -41,6 +41,26 @@ class Student < Person git: git ) end + + def self.read_from_txt(file_path) + raise IOError, "File path is invalid or file does not exist: #{file_path}" unless File.exist?(file_path) + + students = [] + + File.foreach(file_path) do |line| + line.strip! + next if line.empty? # Пропустить пустые строки + + begin + student = from_string(line) + students << student + rescue ArgumentError => e + puts "Error processing line: '#{line}'. Reason: #{e.message}" + end + end + + students + end def surname_and_initials "#{@surname} #{name[0]}.#{patronymic[0]}."