Compare commits

...

19 Commits
lab3 ... main

Author SHA1 Message Date
Artem-Darius Weber 308812f0a4 Merge branch 'lab1-archive'
2 months ago
Artem-Darius Weber 103cbbb25d Add main program to execute specific methods based on command-line arguments and read array from file
2 months ago
Artem-Darius Weber 386361564b Implement methods to find minimum, first positive index, and first positive element with for and while loops
2 months ago
Artem-Darius Weber 9b41aa5119 Merge branch 'main' of https://git.djft.ru/darius-atlas/kubsu-sm5-ruby
4 months ago
Artem-Darius Weber 99186f1d0f docs: added link on Lab2 dir to Readme
4 months ago
Artem-Darius Weber 5fd9a5a36e Merge lab1 and lab2
4 months ago
Artem-Darius Weber effdf6a672 Merge pull request 'git-fix: restore old files from lab1' (#5) from lab1-archive into main
4 months ago
Artem-Darius Weber 6015865dc2 git-fix: make only lab1 branch (restore by old commits)
4 months ago
Artem-Darius Weber bd196668af Merge pull request 'lab2 merge to main' (#4) from lab2 into main
4 months ago
Artem-Darius Weber dc752cdf43 feat: add write_txt method to Student class
4 months ago
Artem-Darius Weber acf69c4385 feat: Add read_from_txt method to Student class
4 months ago
Artem-Darius Weber d387e12d7d ref: Extract common functionality into Person superclass
4 months ago
Artem-Darius Weber 68ca94d4e9 feat: add StudentShort class with immutable fields and constructors
4 months ago
Artem-Darius Weber 611c712b8a feat: add `get_info` method and protected access to key student info
4 months ago
Artem-Darius Weber 8858731565 feat: add constructor for Student class to parse object from string representation
4 months ago
Artem-Darius Weber 1407d3e6fd feat: Add contact validation and secure modification for Student class
4 months ago
Artem-Darius Weber c1ca0ea845 feat: add validation methods for Git link and contact presence
4 months ago
Artem-Darius Weber fca36ccd9d feat: add field validations to Student class and modify constructor
4 months ago
Artem-Darius Weber 10362d3aaf ref: delete lab1 code from lab2 branch
4 months ago

@ -9,6 +9,7 @@ Abstract:
Content:
[Lab 1: Hello world, user interface and number funcations](https://git.djft.ru/darius-atlas/kubsu-sm5-ruby/src/branch/main/lab1)
[Lab 2: Person classes](https://git.djft.ru/darius-atlas/kubsu-sm5-ruby/src/branch/main/lab2)
---

@ -60,6 +60,41 @@ Task:
ruby 04_number_funcs.rb
```
## 05. Number items operations
Task:
Написать методы, которые находят минимальный, элементы,
номер первого положительного элемента. Каждая операция в отдельном
методе. Решить задачу с помощью циклов(for и while).
```bash
ruby 05_number_items_operations.rb
```
## 06. Command line file arguments
Task:
Написать программу, которая принимает как аргумент два
значения. Первое значение говорит, какой из методов задачи 1
выполнить, второй говорит о том, откуда читать список аргументом
должен быть написан адрес файла. Далее необходимо прочитать массив
и выполнить метод.
```bash
ruby 06_command_line_file_arguments.rb min numbers.txt
```
```bash
ruby 06_command_line_file_arguments.rb first_positive numbers.txt
```
```bash
ruby 06_command_line_file_arguments.rb first_positive_index numbers.txt
```
---
Author: Artem-Darius Weber

@ -0,0 +1,68 @@
def find_min_element_for(arr)
min_element = arr[0]
for element in arr
min_element = element if element < min_element
end
min_element
end
def find_min_element_while(arr)
min_element = arr[0]
index = 0
while index < arr.size
min_element = arr[index] if arr[index] < min_element
index += 1
end
min_element
end
def find_first_positive_index_for(arr)
for index in 0...arr.size
return index if arr[index] > 0
end
nil
end
def find_first_positive_index_while(arr)
index = 0
while index < arr.size
return index if arr[index] > 0
index += 1
end
nil
end
def find_first_positive_for(arr)
for element in arr
return element if element > 0
end
nil
end
def find_first_positive_while(arr)
index = 0
while index < arr.size
return arr[index] if arr[index] > 0
index += 1
end
nil
end
# INPUT
array = [-10, -5, 0, 3, 5, -2]
puts "Минимальный элемент (for): #{find_min_element_for(array)}"
puts "Минимальный элемент (while): #{find_min_element_while(array)}"
puts "Индекс первого положительного элемента (for): #{find_first_positive_index_for(array)}"
puts "Индекс первого положительного элемента (while): #{find_first_positive_index_while(array)}"
puts "Первый положительный элемент (for): #{find_first_positive_for(array)}"
puts "Первый положительный элемент (while): #{find_first_positive_while(array)}"

@ -0,0 +1,62 @@
def find_min_element(arr)
min_element = arr[0]
arr.each do |element|
min_element = element if element < min_element
end
min_element
end
def find_first_positive_index(arr)
arr.each_with_index do |element, index|
return index if element > 0
end
nil
end
def find_first_positive(arr)
arr.each do |element|
return element if element > 0
end
nil
end
def main
method_name = ARGV[0]
file_path = ARGV[1]
if method_name.nil? || file_path.nil?
puts "Неправльный формат ввода. Использование: ruby program.rb <method_name> <file_path>"
puts "method_name: 'min', 'first_positive_index' или 'first_positive'"
exit
end
begin
array = File.read(file_path).split.map(&:to_i)
rescue Errno::ENOENT
puts "Файл не найден: #{file_path}"
exit
end
case method_name
when 'min'
result = find_min_element(array)
puts "Минимальный элемент: #{result}"
when 'first_positive_index'
result = find_first_positive_index(array)
puts "Индекс первого положительного элемента: #{result.nil? ? 'Нет положительного элемента' : result}"
when 'first_positive'
result = find_first_positive(array)
puts "Первый положительный элемент: #{result.nil? ? 'Нет положительного элемента' : result}"
else
puts "Неизвестный метод: #{method_name}"
puts "Доступные методы: 'min', 'first_positive_index', 'first_positive'"
end
end
main if __FILE__ == $0

@ -0,0 +1 @@
-10 -5 0 3 5 -2

@ -1,27 +1,59 @@
require_relative 'student'
require_relative 'student_short'
begin
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}"
student1 = Student.new(
surname: 'Алексеевич',
name: 'Артем-Дариус',
patronymic: 'Вебер',
id: 1,
git: 'https://github.com/space-creator'
)
student1.set_contacts(
phone: '+79891242223',
telegram: '@alstroemeria22',
email: 'no-replay@djft.ru',
git: 'https://git.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(
surname: 'nil',
surname: 'Норакет',
name: 'Норакет',
patronymic: 'nil'
patronymic: 'Фамилия'
)
student2.set_contacts(
phone: '+70000000000'
)
puts student1
puts '-' * 40
puts student2
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 "Err.: #{e.message}"
puts "Ошибка: #{e.message}"
end

@ -0,0 +1,65 @@
class Person
attr_accessor :id, :git
def initialize(args = {})
@id = args[:id] || nil
@git = args[:git]
validate
end
def set_contacts(phone: nil, telegram: nil, email: nil)
@phone = phone
raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !self.class.valid_phone_number?(@phone)
@telegram = telegram
raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !self.class.valid_telegram?(@telegram)
@email = email
raise ArgumentError, "Invalid email format: #{@email}" if @email && !self.class.valid_email?(@email)
end
def self.valid_phone_number?(phone)
phone.match?(/\A\+?[0-9]{10,15}\z/)
end
def self.valid_name?(name)
name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/)
end
def self.valid_telegram?(telegram)
telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/)
end
def self.valid_email?(email)
email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/)
end
def self.valid_git?(git)
git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/)
end
def git_present?
!@git.nil? && !@git.empty?
end
def contact_present?
!(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?)
end
def validate
raise ArgumentError, "Git link is required" unless git_present?
raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present?
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

@ -1,33 +1,92 @@
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
require_relative 'person'
class Student < Person
attr_accessor :surname, :name, :patronymic
def initialize(args = {})
@surname = args.fetch(:surname)
@name = args.fetch(:name)
@patronymic = args.fetch(:patronymic)
@id = args[:id] || nil
super(args)
@surname = args.fetch(:surname)
raise ArgumentError, "Invalid surname format: #{@surname}" unless self.class.valid_name?(@surname)
@name = args.fetch(:name)
raise ArgumentError, "Invalid name format: #{@name}" unless self.class.valid_name?(@name)
@patronymic = args.fetch(:patronymic)
raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless self.class.valid_name?(@patronymic)
set_contacts(
phone: args[:phone],
telegram: args[:telegram],
email: args[:email]
)
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
)
end
@phone = args[:phone]
if @phone && !Student.valid_phone_number?(@phone)
raise ArgumentError, "Invalid phone number format: #{@phone}"
def self.read_from_txt(file_path)
raise IOError, "File path is invalid or file does not exist: #{file_path}" unless File.exist?(file_path)
students = []
File.foreach(file_path) do |line|
line.strip!
next if line.empty?
begin
student = from_string(line)
students << student
rescue ArgumentError => e
puts "Error processing line: '#{line}'. Reason: #{e.message}"
end
end
@telegram = args[:telegram] || nil
@email = args[:email] || nil
@git = args[:git] || nil
students
end
def self.write_to_txt(file_path, students)
raise ArgumentError, "Expected an array of Student objects" unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
File.open(file_path, 'w') do |file|
students.each do |student|
file.puts student.to_s
end
end
puts "Data successfully written to #{file_path}"
rescue IOError => e
puts "An error occurred while writing to the file: #{e.message}"
end
def surname_and_initials
"#{@surname} #{name[0]}.#{patronymic[0]}."
end
def to_s
"Student: #{@surname} #{@name} #{@patronymic}\n" \
"ID: #{@id || 'N/A'}\n" \
"Phone: #{@phone || 'N/A'}\n" \
"Telegram: #{@telegram || 'N/A'}\n" \
"Email: #{@email || 'N/A'}\n" \
"Git: #{@git || 'N/A'}"
"#{@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
end
end

@ -0,0 +1,31 @@
require_relative 'person'
class StudentShort < Person
attr_reader :surname_initials, :contact
def initialize(student)
super(id: student.id, git: student.git_info)
@surname_initials = student.surname_and_initials
@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…
Cancel
Save