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.
pull/4/head
Artem-Darius Weber 4 months ago
parent 8858731565
commit 611c712b8a

@ -20,6 +20,11 @@ begin
email: 'no-replay@djft.ru' 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( student2 = Student.new(
surname: 'Норакет', surname: 'Норакет',
name: 'Норакет', name: 'Норакет',

@ -44,26 +44,26 @@ class Student
validate validate
end end
def self.from_string(student_string) def self.from_string(student_string)
parts = student_string.split('|').map(&:strip) parts = student_string.split('|').map(&:strip)
surname, name, patronymic = parts[0].split(' ') surname, name, patronymic = parts[0].split(' ')
id = parts[1].split(': ').last.to_i id = parts[1].split(': ').last.to_i
phone = parts[2].split(': ').last phone = parts[2].split(': ').last
telegram = parts[3].split(': ').last telegram = parts[3].split(': ').last
email = parts[4].split(': ').last email = parts[4].split(': ').last
git = parts[5].split(': ').last git = parts[5].split(': ').last
new( new(
surname: surname, surname: surname,
name: name, name: name,
patronymic: patronymic, patronymic: patronymic,
id: id, id: id,
phone: phone, phone: phone,
telegram: telegram, telegram: telegram,
email: email, email: email,
git: git git: git
) )
end end
def set_contacts(phone: nil, telegram: nil, email: nil) def set_contacts(phone: nil, telegram: nil, email: nil)
@ -91,14 +91,33 @@ class Student
end end
def to_s def to_s
"#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \ "#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \
"Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \ "Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \
"Email: #{@email || 'N/A'} | Git: #{@git || 'N/A'}" "Email: #{@email || 'N/A'} | Git: #{@git || 'N/A'}"
end 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 private
attr_reader :phone, :telegram, :email attr_reader :phone, :telegram, :email
attr_writer :phone, :telegram, :email attr_writer :phone, :telegram, :email
end end
Loading…
Cancel
Save