From 3fa2ac306e4f736d61218592070eea488fda26a7 Mon Sep 17 00:00:00 2001 From: Yarmolenko Daniil Date: Tue, 16 Oct 2018 08:43:01 +0300 Subject: [PATCH 1/2] add methods --- .idea/lessson1.iml | 27 +++++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 245 +++++++++++++++++++++++++++++++++++++++++++ lesson1.rb | 22 +++- my_array.rb | 47 ++++++--- spec/lesson1_spec.rb | 2 +- test.rb | 3 + 9 files changed, 349 insertions(+), 18 deletions(-) create mode 100644 .idea/lessson1.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 test.rb diff --git a/.idea/lessson1.iml b/.idea/lessson1.iml new file mode 100644 index 0000000..3570bdb --- /dev/null +++ b/.idea/lessson1.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6ea7350 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..151e4dc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..a3bc860 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,245 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - 1539628148466 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/lesson1.rb b/lesson1.rb index bcd571c..ed28717 100644 --- a/lesson1.rb +++ b/lesson1.rb @@ -7,15 +7,13 @@ def sum(val = 0) end def age(birthday) - d, m, y = birthday.to_s.split('/') - is_valid = Date.valid_date? y.to_i, m.to_i, d.to_i - if is_valid + if birthday.is_a? String days = (Date.parse(birthday) - Date.today).to_i.abs years = days / 365 hours = days * 24 minutes = hours * 60 - second = minutes * 60 - "Я живу #{years} года или #{days} дней или #{hours} часов или #{minutes} минут или #{second} секунд" + "Я живу #{years} года или #{days} дней или #{hours} + часов или #{minutes} минут или #{minutes * 60} секунд" else 'Invalid Date Format' end diff --git a/my_array.rb b/my_array.rb index deda435..b05ce98 100644 --- a/my_array.rb +++ b/my_array.rb @@ -6,19 +6,43 @@ def initialize(arr = []) end def size - @array.size + # @array.size + size = 0 + @array.each { size += 1 } + size end def reverse - @array.reverse + # @array.reverse + reverse_arr = [] + @array.each { |n| reverse_arr.unshift(n) } + reverse_arr end def max - @array.max + # @array.max + max_value = nil + @array.each do |n| + if max_value.nil? + max_value = n + elsif max_value < n + max_value = n + end + end + max_value end def min - @array.min + # @array.min + min_value = nil + @array.each do |n| + if min_value.nil? + min_value = n + elsif min_value > n + min_value = n + end + end + min_value end def desc @@ -55,9 +79,10 @@ def devide_on_ten end def chars - chars_arr = [] - @array.each { |n| chars_arr.push(n.chr.to_sym)} - chars_arr + chars_arr = ('a'..'z').map(&:to_s) + result = [] + @array.each { |n| result.push(chars_arr[n - 1].to_sym) } + result end def switch @@ -72,7 +97,7 @@ def switch end def before_min - min_index = a.index(a.min) + min_index = @array.index(@array.min) @array[0...min_index] end diff --git a/test.rb b/test.rb deleted file mode 100644 index c2ede40..0000000 --- a/test.rb +++ /dev/null @@ -1,3 +0,0 @@ -a = [10, 6, 3, 4, 5, 6, 7, 8, 9, 10] -min_index = a.index(a.min) -puts a[0...min_index] \ No newline at end of file