Advertisement
Guest User

Untitled

a guest
Aug 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.91 KB | None | 0 0
  1. require_relative "to_s"
  2. class Disc < Product
  3.   attr_accessor :title, :genre, :author, :year
  4.  
  5.   def self.from_file(file_path)
  6.     if File.exist?(file_path)
  7.       # переменной strings присваиваем весь файл в виде массива строк
  8.       strings = File.readlines(file_path).map { |string| string.chomp }
  9.  
  10.       self.new(
  11.         :title => strings[0],
  12.         :author => strings[1],
  13.         :genre => strings[2],
  14.         :year => strings[3].to_i,
  15.         :price => strings[4].to_i,
  16.         :quantity => strings[5].to_i
  17.       )
  18.     else
  19.       abort "Файл не найден!"
  20.     end
  21.   end
  22.  
  23.   def initialize(param)
  24.     super
  25.     @title = param[:title]
  26.     @genre = param[:genre]
  27.     @author = param[:author]
  28.     @year = param[:year]
  29.   end
  30.  
  31.   include To_s
  32.  
  33.   def to_s_for_cart
  34.     "Диск <#{@title}> - #{@author},  #{@genre}, #{@year} года"
  35.   end
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement