- Created StudentShort class with fields: ID, surname initials, git, and contact, which cannot be modified directly. - Implemented two constructors: 1. One that accepts a Student object to initialize fields. 2. Another that accepts ID and a string containing surname initials, git, and contact information. - Updated main.rb to test both constructors of StudentShort, ensuring proper field initialization and immutability.pull/4/head
parent
611c712b8a
commit
68ca94d4e9
@ -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
|
Loading…
Reference in new issue