From 611c712b8a068a783ac4325413b5a2bc018e8257 Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Sat, 21 Sep 2024 17:46:44 +0300 Subject: [PATCH] feat: add `get_info` method and protected access to key student info - Added `get_info` method to return a concise summary of the student, including surname with initials, Git link, and primary contact method. - Implemented individual methods for retrieving surname with initials, Git link, and contact information separately. - Protected contact fields from direct modification; fields can now only be updated via `set_contacts`. - Enhanced string parsing with `from_string` constructor to initialize objects directly from formatted strings. - Updated main file to test new methods and constructor functionality. --- lab2/main.rb | 5 ++++ lab2/student.rb | 69 +++++++++++++++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/lab2/main.rb b/lab2/main.rb index 021879e..d9848cf 100644 --- a/lab2/main.rb +++ b/lab2/main.rb @@ -20,6 +20,11 @@ begin email: 'no-replay@djft.ru' ) + puts student1.get_info + puts "Surname and Initials: #{student1.surname_and_initials}" + puts "Git Info: #{student1.git_info}" + puts "Contact Info: #{student1.contact_info}" + student2 = Student.new( surname: 'Норакет', name: 'Норакет', diff --git a/lab2/student.rb b/lab2/student.rb index 62a14ac..d02f868 100644 --- a/lab2/student.rb +++ b/lab2/student.rb @@ -44,26 +44,26 @@ 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 - ) + 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) @@ -91,14 +91,33 @@ class Student end def to_s - "#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \ - "Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \ - "Email: #{@email || 'N/A'} | 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 + + def get_info + "#{surname_and_initials}, Git: #{git_info}, Contact: #{contact_info}" + end + + def surname_and_initials + "#{@surname} #{name[0]}.#{patronymic[0]}." + end + + def git_info + @git + end + + def contact_info + return "Phone: #{@phone}" if @phone + return "Telegram: #{@telegram}" if @telegram + return "Email: #{@email}" if @email + 'No contact available' + end private attr_reader :phone, :telegram, :email - attr_writer :phone, :telegram, :email -end \ No newline at end of file + end + \ No newline at end of file