diff --git a/lab2/main.rb b/lab2/main.rb index 5b42be6..b426934 100644 --- a/lab2/main.rb +++ b/lab2/main.rb @@ -1,22 +1,27 @@ require_relative 'student' -student1 = Student.new( - surname: 'Алексеевич', - name: 'Артем-Дариус', - patronymic: 'Вебер', - id: 1, - phone: '+79891242223', - telegram: '@alstroemeria22', - email: 'no-replay@djft.ru', - git: 'https://git.djft.ru' -) +begin + student1 = Student.new( + surname: 'Алексеевич', + name: 'Артем-Дариус', + patronymic: 'Вебер', + id: 1, + phone: '+79891242223', + telegram: '@alstroemeria22', + email: 'no-replay@djft.ru', + git: 'https://git.djft.ru' + ) -student2 = Student.new( - surname: 'nil', - name: 'Норакет', - patronymic: 'nil' -) + student2 = Student.new( + surname: 'nil', + name: 'Норакет', + patronymic: 'nil' + ) + + puts student1 + puts '-' * 40 + puts student2 +rescue ArgumentError => e + puts "Err.: #{e.message}" +end -puts student1 -puts '-' * 40 -puts student2 diff --git a/lab2/student.rb b/lab2/student.rb index 0ba74f2..14f5464 100644 --- a/lab2/student.rb +++ b/lab2/student.rb @@ -1,14 +1,22 @@ class Student attr_accessor :id, :surname, :name, :patronymic, :phone, :telegram, :email, :git + def self.valid_phone_number?(phone) + phone.match?(/\A\+?[0-9]{10,15}\z/) + end + def initialize(args = {}) @surname = args.fetch(:surname) @name = args.fetch(:name) @patronymic = args.fetch(:patronymic) - @id = args[:id] || nil - @phone = args[:phone] || nil + + @phone = args[:phone] + if @phone && !Student.valid_phone_number?(@phone) + raise ArgumentError, "Invalid phone number format: #{@phone}" + end + @telegram = args[:telegram] || nil @email = args[:email] || nil @git = args[:git] || nil