commit
bd196668af
@ -1,16 +0,0 @@
|
|||||||
require_relative '../src/02_user_interface.rb'
|
|
||||||
|
|
||||||
RSpec.describe "Main" do
|
|
||||||
before do
|
|
||||||
allow(STDIN).to receive(:gets).and_return("ruby\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "greets the user and checks Ruby as language" do
|
|
||||||
expect { main() }.to output("Hello my catgirl test_user! \nWhat is your love language?\nruby\nc++\npy\nПодлиза \n").to_stdout
|
|
||||||
end
|
|
||||||
|
|
||||||
it "handles unknown language input" do
|
|
||||||
allow(STDIN).to receive(:gets).and_return("java\n")
|
|
||||||
expect { main() }.to output(/Неизвестный язык: java/).to_stdout
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,2 +0,0 @@
|
|||||||
puts "Hello, world!"
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
|||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
LANGUAGES = [
|
|
||||||
"ruby",
|
|
||||||
"c++",
|
|
||||||
"py"
|
|
||||||
]
|
|
||||||
|
|
||||||
def main()
|
|
||||||
user_name = ARGV[0]
|
|
||||||
puts "Hello my catgirl #{user_name}! \nWhat is your love language?"
|
|
||||||
LANGUAGES.each { |language| puts "#{language}", " " }
|
|
||||||
|
|
||||||
user_lang = STDIN.gets.chomp
|
|
||||||
|
|
||||||
case user_lang
|
|
||||||
when "ruby"
|
|
||||||
puts "Подлиза \n"
|
|
||||||
when "c++"
|
|
||||||
puts "MATLAB скушал? \n"
|
|
||||||
when "TS/JS"
|
|
||||||
puts "Фронтендер, Фууу! \n"
|
|
||||||
when "py"
|
|
||||||
puts "Девопсер, иди ДАГИ писать \n"
|
|
||||||
else
|
|
||||||
puts "Неизвестный язык: #{user_lang}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
main()
|
|
@ -1,39 +0,0 @@
|
|||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
LANGUAGES = [
|
|
||||||
"ruby",
|
|
||||||
"c++",
|
|
||||||
"py"
|
|
||||||
]
|
|
||||||
|
|
||||||
def main()
|
|
||||||
user_name = ARGV[0]
|
|
||||||
puts "Hello my catgirl #{user_name}! \nWhat is your love language?"
|
|
||||||
LANGUAGES.each { |language| puts "#{language}", " " }
|
|
||||||
|
|
||||||
user_lang = STDIN.gets.chomp
|
|
||||||
case user_lang
|
|
||||||
when "ruby"
|
|
||||||
puts "Подлиза \n"
|
|
||||||
when "c++"
|
|
||||||
puts "MATLAB скушал? \n"
|
|
||||||
when "TS/JS"
|
|
||||||
puts "Фронтендер, Фууу! \n"
|
|
||||||
when "py"
|
|
||||||
puts "Девопсер, иди ДАГИ писать \n"
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "Введите команду на языке Ruby для выполнения:"
|
|
||||||
ruby_command = STDIN.gets.chomp
|
|
||||||
begin
|
|
||||||
eval(ruby_command)
|
|
||||||
rescue Exception => e
|
|
||||||
puts "Ошибка выполнения команды Ruby: #{e.message}"
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "Введите команду операционной системы для выполнения:"
|
|
||||||
os_command = STDIN.gets.chomp
|
|
||||||
system(os_command)
|
|
||||||
end
|
|
||||||
|
|
||||||
main()
|
|
@ -1,33 +1,92 @@
|
|||||||
class Student
|
require_relative 'person'
|
||||||
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
|
|
||||||
|
|
||||||
|
class Student < Person
|
||||||
|
attr_accessor :surname, :name, :patronymic
|
||||||
|
|
||||||
def initialize(args = {})
|
def initialize(args = {})
|
||||||
@surname = args.fetch(:surname)
|
super(args)
|
||||||
@name = args.fetch(:name)
|
@surname = args.fetch(:surname)
|
||||||
@patronymic = args.fetch(:patronymic)
|
raise ArgumentError, "Invalid surname format: #{@surname}" unless self.class.valid_name?(@surname)
|
||||||
|
|
||||||
@id = args[:id] || nil
|
@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]
|
def self.read_from_txt(file_path)
|
||||||
if @phone && !Student.valid_phone_number?(@phone)
|
raise IOError, "File path is invalid or file does not exist: #{file_path}" unless File.exist?(file_path)
|
||||||
raise ArgumentError, "Invalid phone number format: #{@phone}"
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
@telegram = args[:telegram] || nil
|
students
|
||||||
@email = args[:email] || nil
|
|
||||||
@git = args[:git] || nil
|
|
||||||
end
|
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
|
def to_s
|
||||||
"Student: #{@surname} #{@name} #{@patronymic}\n" \
|
"#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \
|
||||||
"ID: #{@id || 'N/A'}\n" \
|
"Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \
|
||||||
"Phone: #{@phone || 'N/A'}\n" \
|
"Email: #{@email || 'N/A'} | Git: #{@git || 'N/A'}"
|
||||||
"Telegram: #{@telegram || 'N/A'}\n" \
|
end
|
||||||
"Email: #{@email || 'N/A'}\n" \
|
|
||||||
"Git: #{@git || 'N/A'}"
|
def get_info
|
||||||
|
"#{surname_and_initials}, Git: #{git_info}, Contact: #{contact_info}"
|
||||||
end
|
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…
Reference in new issue