-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rb
More file actions
53 lines (43 loc) · 1 KB
/
Copy pathparser.rb
File metadata and controls
53 lines (43 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class BaseParser
attr_reader :output
def initialize (filename)
@filename = filename
@numbers = []
end
def check (data)
if valid?(data)
return data
else
raise "File incorrect!"
end
end
#проверить на валидность. ексепшн
def valid? (num)
condition = /^\d*$/
return condition.match?(num)
end
#посчитать сумму и среднее
def calculations
sum = 0
@numbers.each {|x| sum += x.to_i }
avrg = sum.to_f / @numbers.length
@output = Array.new [sum, avrg]
end
end
class TxtParser < BaseParser
#прочитать строки
def get_numbers
File.open(@filename).read.each_line do |line|
@numbers << check(line.strip)
end
end
end
class CsvParser < BaseParser
#отделить числа от дат
def get_numbers
File.open(@filename).read.each_line do |line|
@numbers << check(line.split(',')[1].strip)
end
@numbers.shift
end
end