diff --git a/lab2/main.rb b/lab2/main.rb index d9848cf..e714667 100644 --- a/lab2/main.rb +++ b/lab2/main.rb @@ -1,7 +1,8 @@ require_relative 'student' +require_relative 'student_short' begin - student_string = "Норакет Норакет Норакет | ID: 2 | Phone: +1234567890 | Telegram: @nora | Email: nora@example.com | Git: https://github.com/nora" + student_string = "Норакет Норакет Норакет | ID: 2 | Phone: +1234567890 | Telegram: @noracat | Email: nora@example.com | Git: https://github.com/nora" student_from_string = Student.from_string(student_string) puts "Создан объект из строки:\n#{student_from_string}" @@ -11,7 +12,7 @@ begin name: 'Артем-Дариус', patronymic: 'Вебер', id: 1, - git: 'https://git.djft.ru' + git: 'https://github.com/space-creator' ) student1.set_contacts( @@ -38,6 +39,21 @@ begin puts student1 puts '-' * 40 puts student2 + + + student_short_from_student = StudentShort.new(student1) + puts "StudentShort from Student object:" + puts "ID: #{student_short_from_student.id}" + puts "Surname and Initials: #{student_short_from_student.surname_initials}" + puts "Git: #{student_short_from_student.git}" + puts "Contact: #{student_short_from_student.contact}" + + student_short_from_string = StudentShort.from_string(5, 'Skye A.A., Git: https://github.com/skye, Contact: Phone: +4923467890') + puts "StudentShort from string:" + puts "ID: #{student_short_from_string.id}" + puts "Surname and Initials: #{student_short_from_string.surname_initials}" + puts "Git: #{student_short_from_string.git}" + puts "Contact: #{student_short_from_string.contact}" rescue ArgumentError => e puts "Ошибка: #{e.message}" end diff --git a/lab2/student_short.rb b/lab2/student_short.rb new file mode 100644 index 0000000..85f9762 --- /dev/null +++ b/lab2/student_short.rb @@ -0,0 +1,30 @@ +class StudentShort + attr_reader :id, :surname_initials, :git, :contact + + def initialize(student) + @id = student.id + @surname_initials = student.surname_and_initials + @git = student.git_info + @contact = student.contact_info + end + + def self.from_string(id, info_string) + parts = info_string.split(', ').map(&:strip) + surname_initials = parts[0] + git = parts[1].split(': ').last + contact = parts[2].split(': ').last + + new_instance = allocate + new_instance.send(:initialize_from_data, id, surname_initials, git, contact) + new_instance + end + + private + + def initialize_from_data(id, surname_initials, git, contact) + @id = id + @surname_initials = surname_initials + @git = git + @contact = contact + end +end