diff --git a/lab2/main.rb b/lab2/main.rb index 8233460..021879e 100644 --- a/lab2/main.rb +++ b/lab2/main.rb @@ -1,33 +1,38 @@ require_relative 'student' begin - student1 = Student.new( - surname: 'Алексеевич', - name: 'Артем-Дариус', - patronymic: 'Вебер', - id: 1, - git: 'https://git.djft.ru' - ) + student_string = "Норакет Норакет Норакет | ID: 2 | Phone: +1234567890 | Telegram: @nora | Email: nora@example.com | Git: https://github.com/nora" + student_from_string = Student.from_string(student_string) + puts "Создан объект из строки:\n#{student_from_string}" - student1.set_contacts( - phone: '+79891242223', - telegram: '@alstroemeria22', - email: 'no-replay@djft.ru' - ) - student2 = Student.new( - surname: 'Норакет', - name: 'Норакет', - patronymic: 'Фамилия' - ) + student1 = Student.new( + surname: 'Алексеевич', + name: 'Артем-Дариус', + patronymic: 'Вебер', + id: 1, + git: 'https://git.djft.ru' + ) - student2.set_contacts( - phone: '+70000000000' - ) + student1.set_contacts( + phone: '+79891242223', + telegram: '@alstroemeria22', + email: 'no-replay@djft.ru' + ) - puts student1 - puts '-' * 40 - puts student2 + student2 = Student.new( + surname: 'Норакет', + name: 'Норакет', + patronymic: 'Фамилия' + ) + + student2.set_contacts( + phone: '+70000000000' + ) + + puts student1 + puts '-' * 40 + puts student2 rescue ArgumentError => e puts "Ошибка: #{e.message}" end diff --git a/lab2/student.rb b/lab2/student.rb index 83dd59a..62a14ac 100644 --- a/lab2/student.rb +++ b/lab2/student.rb @@ -44,6 +44,27 @@ class Student validate end + + def self.from_string(student_string) + parts = student_string.split('|').map(&:strip) + surname, name, patronymic = parts[0].split(' ') + id = parts[1].split(': ').last.to_i + phone = parts[2].split(': ').last + telegram = parts[3].split(': ').last + email = parts[4].split(': ').last + git = parts[5].split(': ').last + + new( + surname: surname, + name: name, + patronymic: patronymic, + id: id, + phone: phone, + telegram: telegram, + email: email, + git: git + ) + end def set_contacts(phone: nil, telegram: nil, email: nil) @phone = phone @@ -70,13 +91,10 @@ class Student end def to_s - "Student: #{@surname} #{@name} #{@patronymic}\n" \ - "ID: #{@id || 'N/A'}\n" \ - "Phone: #{@phone || 'N/A'}\n" \ - "Telegram: #{@telegram || 'N/A'}\n" \ - "Email: #{@email || 'N/A'}\n" \ - "Git: #{@git || 'N/A'}" - end + "#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \ + "Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \ + "Email: #{@email || 'N/A'} | Git: #{@git || 'N/A'}" + end private