From c9ddb5aae20510930b3a14cbfd93c4f1afece8cb Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Thu, 16 Jul 2020 19:43:02 +0300 Subject: [PATCH 01/55] update gitignore file --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index e3200e0..5c7477a 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,6 @@ build-iPhoneSimulator/ # Used by RuboCop. Remote config files pulled in from inherit_from directive. # .rubocop-https?--* + +.history +.vscode From 5cc7c7a8d57d50a7762f4bff8c5a2bca461c406a Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Thu, 16 Jul 2020 19:44:10 +0300 Subject: [PATCH 02/55] add lib folder and main.rb file --- lib/main.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/main.rb diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 0000000..e69de29 From 26380988c6e5984fafbd4b3601cea3dbc0db1c33 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Thu, 16 Jul 2020 19:45:45 +0300 Subject: [PATCH 03/55] add Enumarable module and my_each method --- lib/main.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index e69de29..29278d8 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -0,0 +1,11 @@ +module Enumerable + def my_each + return enum_for unless block_given? + array = is_a?(Range) ? to_a : self + + for item in array + yield(item) + end + array + end +end \ No newline at end of file From 38851756f72d4e67803d9fa8a41568ce8312f2af Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Thu, 16 Jul 2020 19:46:43 +0300 Subject: [PATCH 04/55] add my_each_with_index method --- lib/main.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index 29278d8..42bc1c5 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -8,4 +8,16 @@ def my_each end array end + + def my_each_with_index + return enum_for unless block_given? + array = is_a?(Range) ? to_a : self + + index = -1 + for item in array + yield(item, index+=1) + end + array + end + end \ No newline at end of file From d127c2fb5ae0ad7d6b5b0cb22fb5cfa5377d0f8e Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Thu, 16 Jul 2020 22:56:00 +0300 Subject: [PATCH 05/55] updated my_map to enable it to accept Proc --- lib/main.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 42bc1c5..23f026a 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -19,5 +19,22 @@ def my_each_with_index end array end + + def my_map(prok = nil) + # return enum_for unless block_given? + array = is_a?(Range) ? to_a : self -end \ No newline at end of file + filtered = [] + if prok + for item in array + filtered.push(prok.call(item)) + end + else + for item in array + filtered.push(yield(item)) + end + end + + filtered + end +end From c53e2d1255c4a90f76d35dc54563497e38f35058 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 00:13:14 +0300 Subject: [PATCH 06/55] fix my_map method --- lib/main.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 23f026a..fdc096b 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -21,7 +21,6 @@ def my_each_with_index end def my_map(prok = nil) - # return enum_for unless block_given? array = is_a?(Range) ? to_a : self filtered = [] From 59a8148796dc13c62001c335e37bba2b8108fab1 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 00:15:20 +0300 Subject: [PATCH 07/55] add #my_each_with_index method --- lib/main.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index fdc096b..9cf971c 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -36,4 +36,16 @@ def my_map(prok = nil) filtered end + + def my_each_with_index + return enum_for unless block_given? + array = is_a?(Range) ? to_a : self + + index = -1 + for item in array + yield(item, index+=1) + end + array + end + end From 24b8f8d74c39ebce6366a1773695485497b457d6 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 00:16:09 +0300 Subject: [PATCH 08/55] add #my_select method --- lib/main.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index 9cf971c..c4c621c 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -19,6 +19,17 @@ def my_each_with_index end array end + + def my_select + return enum_for unless block_given? + array = is_a?(Range) ? to_a : self + + filtered = [] + for item in array + filtered.push(item) if yield(item) + end + filtered + end def my_map(prok = nil) array = is_a?(Range) ? to_a : self From 92087a496a7c4cc2d17ee00b1315dc03b43600b9 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 00:16:50 +0300 Subject: [PATCH 09/55] add #my_all? method --- lib/main.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index c4c621c..2662cc6 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -31,6 +31,15 @@ def my_select filtered end + def my_all? + flag = true + for item in array + flag = yield(item) + break unless flag + end + flag + end + def my_map(prok = nil) array = is_a?(Range) ? to_a : self From 3203335b58a07c3ae8d087dc6b45185942d2ff41 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 00:17:34 +0300 Subject: [PATCH 10/55] add Enumerable #my_none? method --- lib/main.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index 2662cc6..3d18648 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -39,6 +39,15 @@ def my_all? end flag end + + def my_none? + flag = true + for item in array + flag = yield(item) + break if flag + end + flag + end def my_map(prok = nil) array = is_a?(Range) ? to_a : self From 8d6a37ae186a3b1790a3ac5bf28e7204ed749530 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 00:18:18 +0300 Subject: [PATCH 11/55] add Enumerable #my_count method --- lib/main.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 3d18648..eae9975 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -48,7 +48,20 @@ def my_none? end flag end - + + def my_count(arg = nil) + array = is_a?(Range) ? to_a : self + if arg + return array.select{|val| val if val == arg}.length + end + return array.length unless block_given? + count = array.length + for item in array + count-=1 unless yield(item) + end + count + end + def my_map(prok = nil) array = is_a?(Range) ? to_a : self From af2bfd9dec27bc1478a14ed2ddd5f3f0621c180e Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 02:00:36 +0300 Subject: [PATCH 12/55] add Enumarator #my_inject method --- lib/main.rb | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index eae9975..af68dd4 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -89,5 +89,32 @@ def my_each_with_index end array end + + def my_inject(init = nil) + array = is_a?(Range) ? to_a : self + + acc = init + + unless init.is_a?(Integer) + acc = array[0] + array.shift + end + + if init.is_a?(Symbol) + for item in array do + acc = acc.send(init, item) + end + return acc + end + + for item in array do + acc = yield(acc, item) + end + acc + end + + def multiply_els ary + ary.my_inject { |mul, n| mul * n } + end -end +end \ No newline at end of file From 6957275317f750209075cb01f0fd0f3dcf8b2aba Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 02:14:42 +0300 Subject: [PATCH 13/55] update Enumarator #multiply_els method --- lib/main.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index af68dd4..b5e71b4 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -113,8 +113,10 @@ def my_inject(init = nil) acc end - def multiply_els ary - ary.my_inject { |mul, n| mul * n } + def multiply_els + self.my_inject { |mul, n| mul * n } end -end \ No newline at end of file +end + +print [2,4,5].multiply_els \ No newline at end of file From 319adbae182efff1f9cb0629e8647040cf98a442 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 02:59:58 +0300 Subject: [PATCH 14/55] updated Enumarator #my_each linting issues --- .github/workflows/linters.yml | 19 ++++++++++++ .rubocop.yml | 50 +++++++++++++++++++++++++++++++ lib/main.rb | 55 ++++++++++++++++++++--------------- 3 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/linters.yml create mode 100644 .rubocop.yml diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml new file mode 100644 index 0000000..a193d16 --- /dev/null +++ b/.github/workflows/linters.yml @@ -0,0 +1,19 @@ +name: Linters + +on: pull_request + +jobs: + rubocop: + name: Rubocop + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-ruby@v1 + with: + ruby-version: 2.6.x + - name: Setup Rubocop + run: | + gem install --no-document rubocop:'~>0.81.0' # https://docs.rubocop.org/en/stable/installation/ + [ -f .rubocop.yml ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ruby/.rubocop.yml + - name: Rubocop Report + run: rubocop --color \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..4e8ebd6 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,50 @@ +AllCops: + Exclude: + - "Guardfile" + - "Rakefile" + + DisplayCopNames: true + +Layout/LineLength: + Max: 120 +Metrics/MethodLength: + Max: 20 +Metrics/AbcSize: + Max: 50 +Metrics/ClassLength: + Max: 150 +Metrics/BlockLength: + ExcludedMethods: ['describe'] + Max: 30 + + +Style/Documentation: + Enabled: false +Style/ClassAndModuleChildren: + Enabled: false +Style/EachForSimpleLoop: + Enabled: false +Style/AndOr: + Enabled: false +Style/DefWithParentheses: + Enabled: false +Style/FrozenStringLiteralComment: + EnforcedStyle: never + +Layout/HashAlignment: + EnforcedColonStyle: key +Layout/ExtraSpacing: + AllowForAlignment: false +Layout/MultilineMethodCallIndentation: + Enabled: true + EnforcedStyle: indented +Lint/RaiseException: + Enabled: false +Lint/StructNewOverride: + Enabled: false +Style/HashEachMethods: + Enabled: false +Style/HashTransformKeys: + Enabled: false +Style/HashTransformValues: + Enabled: false \ No newline at end of file diff --git a/lib/main.rb b/lib/main.rb index b5e71b4..bc3ec95 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,27 +1,27 @@ module Enumerable def my_each return enum_for unless block_given? + array = is_a?(Range) ? to_a : self - for item in array - yield(item) - end - array + array.map { |item| yield(item) } end def my_each_with_index return enum_for unless block_given? + array = is_a?(Range) ? to_a : self index = -1 for item in array - yield(item, index+=1) + yield(item, index += 1) end array end def my_select return enum_for unless block_given? + array = is_a?(Range) ? to_a : self filtered = [] @@ -40,6 +40,14 @@ def my_all? flag end + def my_any? + flag = array.length > 0 + array.my_each do + break if yield(item) + end + flag +end + def my_none? flag = true for item in array @@ -52,12 +60,15 @@ def my_none? def my_count(arg = nil) array = is_a?(Range) ? to_a : self if arg - return array.select{|val| val if val == arg}.length + return array.select{ |val| val if val == arg }.length end - return array.length unless block_given? + count = array.length + + return count unless block_given? + for item in array - count-=1 unless yield(item) + count -= 1 unless yield(item) end count end @@ -68,7 +79,7 @@ def my_map(prok = nil) filtered = [] if prok for item in array - filtered.push(prok.call(item)) + filtered.push(prok.call(item)) end else for item in array @@ -78,22 +89,11 @@ def my_map(prok = nil) filtered end - - def my_each_with_index - return enum_for unless block_given? - array = is_a?(Range) ? to_a : self - - index = -1 - for item in array - yield(item, index+=1) - end - array - end def my_inject(init = nil) array = is_a?(Range) ? to_a : self - acc = init + acc = init unless init.is_a?(Integer) acc = array[0] @@ -114,9 +114,18 @@ def my_inject(init = nil) end def multiply_els - self.my_inject { |mul, n| mul * n } + my_inject { |mul, n| mul * n } end end -print [2,4,5].multiply_els \ No newline at end of file +hash = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5 } +friends = ['Sharon', 'Leo', 'Leila', 'Brian', 'Arun'] + +# my_each +hash.my_each do |value, key| + print value, '-', key + puts '' +end + +puts '' \ No newline at end of file From 531322c0544a0d7880d8ee363651b35105c8949d Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 03:03:11 +0300 Subject: [PATCH 15/55] updated Enumarator #my_each_with_index linting issues --- .gitignore | 2 ++ lib/main.rb | 7 ++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5c7477a..e7ce268 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,5 @@ build-iPhoneSimulator/ .history .vscode + +.github \ No newline at end of file diff --git a/lib/main.rb b/lib/main.rb index bc3ec95..4ea5f6d 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -13,12 +13,9 @@ def my_each_with_index array = is_a?(Range) ? to_a : self index = -1 - for item in array - yield(item, index += 1) - end - array + array.my_each { |item| yield(item, index += 1) } end - + def my_select return enum_for unless block_given? From 5718e1c7fe04c3796f59be6e6f6c7cd84ffdc383 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 03:06:10 +0300 Subject: [PATCH 16/55] fix Enumarator #my_select linting issues --- lib/main.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 4ea5f6d..6266935 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -11,7 +11,6 @@ def my_each_with_index return enum_for unless block_given? array = is_a?(Range) ? to_a : self - index = -1 array.my_each { |item| yield(item, index += 1) } end @@ -20,14 +19,11 @@ def my_select return enum_for unless block_given? array = is_a?(Range) ? to_a : self - filtered = [] - for item in array - filtered.push(item) if yield(item) - end + array.my_each { |item| filtered.push(item) if yield(item) } filtered end - + def my_all? flag = true for item in array From bbed094d67d146137040e29c86ada5391c834b9d Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 03:07:58 +0300 Subject: [PATCH 17/55] fix Enumarator #my_all? linting issues --- lib/main.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 6266935..4ec83de 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -26,7 +26,7 @@ def my_select def my_all? flag = true - for item in array + array.my_each do |item| flag = yield(item) break unless flag end From a2a3858539860c2ce2db11fe02c39535b64ec9b8 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 03:27:44 +0300 Subject: [PATCH 18/55] fix Enumarator #my_aye? linting issues --- .DS_Store | Bin 0 -> 6148 bytes lib/main.rb | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5d775512999c1d52583405ce066b351b03380daf GIT binary patch literal 6148 zcmeHKJxc>Y5Pf3;3D~65a!V`0+Ga^|ML=xw2jmnqaFE9I_jml2@y$nqiB|`a8Q6J~ z**AN)53+j$K<2x*2Ve$Z%AqJ4449?|r>=rLB8si?fgRdaw|#B9frb8JO5c5gd%R>W8n6E7ut zsYHt*Ue0_8UJdLWy&MwFhs2W)&5PL6xqdM^q&lWg1yX@i1+@32&Gi0n_?H=M@{N)% z6-WjCs{%4!u9tJ}F7DP}`_#L(aJ+D+DX-Un#`xqTfIsvcxwT23FB+3q1A9kZMe|lp P%ntz*BwZ?S0R_GR(`_*7 literal 0 HcmV?d00001 diff --git a/lib/main.rb b/lib/main.rb index 4ec83de..f990173 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -26,7 +26,7 @@ def my_select def my_all? flag = true - array.my_each do |item| + array.my_each do |item| flag = yield(item) break unless flag end @@ -34,12 +34,12 @@ def my_all? end def my_any? - flag = array.length > 0 + flag = array.empty? array.my_each do break if yield(item) end flag -end + end def my_none? flag = true From ef53573d66a36a33a3212ba090b6ce197c9a33af Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 03:29:41 +0300 Subject: [PATCH 19/55] fix Enumarator #my_none? linting issues --- lib/main.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index f990173..f5c88f1 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -43,13 +43,13 @@ def my_any? def my_none? flag = true - for item in array + array.my_each do flag = yield(item) break if flag end flag end - + def my_count(arg = nil) array = is_a?(Range) ? to_a : self if arg From ceed52e0144f75036e0c3721fce5253059eacdeb Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 03:58:19 +0300 Subject: [PATCH 20/55] fix rubocop liniting issues --- lib/main.rb | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index f5c88f1..6d12a71 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -52,15 +52,16 @@ def my_none? def my_count(arg = nil) array = is_a?(Range) ? to_a : self + if arg - return array.select{ |val| val if val == arg }.length + return array.my_select { |val| val if val == arg }.length end count = array.length return count unless block_given? - - for item in array + + array.my_each do |item| count -= 1 unless yield(item) end count @@ -71,36 +72,30 @@ def my_map(prok = nil) filtered = [] if prok - for item in array - filtered.push(prok.call(item)) - end + array.my_each { |item| filtered.push(prok.call(item)) } else - for item in array - filtered.push(yield(item)) - end + array.my_each { |item| filtered.push(yield(item)) } end - filtered end - + def my_inject(init = nil) array = is_a?(Range) ? to_a : self acc = init - unless init.is_a?(Integer) acc = array[0] array.shift end if init.is_a?(Symbol) - for item in array do + array.my_each do |item| acc = acc.send(init, item) end return acc end - for item in array do + array.my_each do |item| acc = yield(acc, item) end acc @@ -109,16 +104,4 @@ def my_inject(init = nil) def multiply_els my_inject { |mul, n| mul * n } end - -end - -hash = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5 } -friends = ['Sharon', 'Leo', 'Leila', 'Brian', 'Arun'] - -# my_each -hash.my_each do |value, key| - print value, '-', key - puts '' end - -puts '' \ No newline at end of file From 161b876ad96576f36421f5bba38c31f2a1f2350d Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 04:15:33 +0300 Subject: [PATCH 21/55] add readme file --- README.md | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..44cc569 --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# NEXT MEAL +![Hireable](https://cdn.rawgit.com/hiendv/hireable/master/styles/default/yes.svg) + +> The Enumerable module that gets mixed into the Array and Hash classes (among others) and provides you with lots of handy iterator methods. To prove that there's no magic to it, custom build of those methods. + + +This iterators are built using ruby v2.7 + +## Built With + +- Ruby +- rubocop for linting purpose + +## Live Demo + +- [Live view on repl.it](https://repl.it/@aliabdulaziz/ruby-custom-enumerables) + +## Getting Started + +**Install on Your Own Machine.** +**You can skip the linter setup if you don't want to validate the code against good coding standards.** + +Set up your machine. + +### Prerequisites + +- ruby +- rubocop + +### Setup + +- Follow the link below to install git + > [download git](https://git-scm.com/downloads) +- Follow the link below to install ruby + > [Install Ruby](https://www.theodinproject.com/courses/ruby-programming/lessons/installing-ruby-ruby-programming) +- Follow the link below to set up a linter + > [Set up Linter (rubocop) to check code errors](https://github.com/rubocop-hq/rubocop) +- Clone the repository + > Clone the repository by run the code below on your computer terminal. + `git clone https://github.com/abredi/ruby-custom-enumerables.git` + + + +### Usage + +Go to the project directory and run open the `ruby lib/ruby.rb` file on your terminal. + +## Authors + +👤 **Abdulaziz Ali** + +- Github: [@abredi](https://github.com/abredi) +- Twitter: [@rediabdulaziz](https://twitter.com/rediabdulaziz) +- Linkedin: [linkedin](https://www.linkedin.com/in/abdulaziz-ali-98948011a) + + + +## 🤝 Contributing + +Contributions, issues and feature requests are welcome! + +Feel free to check the [issues page](issues/). + +## Show your support + +Give a ⭐️ if you like this project! + +## Acknowledgments + +- [Thanks for Microverse][https://github.com/microverseinc] +- [Thank you, PatashuleKE, for the design.](https://www.behance.net/gallery/25563385/PatashuleKE) + > I used their web design for this project +- [Thank you, @matheusfrade, for the meal picture.](https://unsplash.com/@matheusfrade) + > I used his meal picture for this project + +## 📝 License + +This project is [MIT](LICENSE) licensed. From 87514e420776768b736f2dd1ce8260798d0425d1 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 20:28:27 +0300 Subject: [PATCH 22/55] update readme file to remove uneccesary contents --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index 44cc569..8151ea0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # NEXT MEAL -![Hireable](https://cdn.rawgit.com/hiendv/hireable/master/styles/default/yes.svg) +![Hireable](https://cdn.rawgit.com/hiendv/hireable/master/styles/default/yes.svg) [![Run on Repl.it](https://repl.it/badge/github/wrakc/Bubble-Sort---Microverse)](https://repl.it/@aliabdulaziz/ruby-custom-enumerables) > The Enumerable module that gets mixed into the Array and Hash classes (among others) and provides you with lots of handy iterator methods. To prove that there's no magic to it, custom build of those methods. @@ -68,10 +68,6 @@ Give a ⭐️ if you like this project! ## Acknowledgments - [Thanks for Microverse][https://github.com/microverseinc] -- [Thank you, PatashuleKE, for the design.](https://www.behance.net/gallery/25563385/PatashuleKE) - > I used their web design for this project -- [Thank you, @matheusfrade, for the meal picture.](https://unsplash.com/@matheusfrade) - > I used his meal picture for this project ## 📝 License From 96435b30f9927e9b0a6d54746ba85daea428d4c6 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 20:29:11 +0300 Subject: [PATCH 23/55] update Enumarable #my_each and #my_each_with_index methods --- lib/main.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index 6d12a71..e849b5d 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -5,6 +5,7 @@ def my_each array = is_a?(Range) ? to_a : self array.map { |item| yield(item) } + array end def my_each_with_index @@ -13,6 +14,7 @@ def my_each_with_index array = is_a?(Range) ? to_a : self index = -1 array.my_each { |item| yield(item, index += 1) } + array end def my_select From fa9688e658868b42b498b0f0c28f09cdef77422b Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 20:29:43 +0300 Subject: [PATCH 24/55] add rspec testing files --- .rspec | 1 + spec/main_spec.rb | 0 spec/spec_helper.rb | 100 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 .rspec create mode 100644 spec/main_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/spec/main_spec.rb b/spec/main_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..251aa51 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,100 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From fdb0d4cef584d286677b186720defe44079b4513 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 20:55:36 +0300 Subject: [PATCH 25/55] add Enumarable #my_each testing --- spec/main_spec.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index e69de29..4a50094 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -0,0 +1,29 @@ +require 'rspec' +require '../lib/main' + +Rspec.describe Enumerable do + describe '#my_each' do + it 'should be equal' do + ary = %w[zero one two] + expect(ary.my_each(&:upcase)).to eql(ary) + end + + it 'should be excute the given block code' do + raw_ary = %w[one two three] + cap = '' + raw_ary.my_each do |item| + cap = item.upcase + end + expect(cap).to eql(raw_ary.last.upcase) + end + + it 'should work with range' do + raw_range = (1..5) + last = 0 + raw_range.my_each do |item| + last = item + end + expect(last).to eql(raw_range.last) + end + end +end From c4ee0059da909e0a45d2e9328bf8ab39a7c5d94f Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 21:46:10 +0300 Subject: [PATCH 26/55] remove unnecessary comments content from spec_helper.rb --- spec/spec_helper.rb | 54 --------------------------------------------- 1 file changed, 54 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 251aa51..0cb0392 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -43,58 +43,4 @@ # inherited by the metadata hash of host groups and examples, rather than # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups - -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. -=begin - # This allows you to limit a spec run to individual examples or groups - # you care about by tagging them with `:focus` metadata. When nothing - # is tagged with `:focus`, all examples get run. RSpec also provides - # aliases for `it`, `describe`, and `context` that include `:focus` - # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - config.filter_run_when_matching :focus - - # Allows RSpec to persist some state between runs in order to support - # the `--only-failures` and `--next-failure` CLI options. We recommend - # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" - - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - config.disable_monkey_patching! - - # This setting enables warnings. It's recommended, but in some cases may - # be too noisy due to issues in dependencies. - config.warnings = true - - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = "doc" - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 - - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = :random - - # Seed global randomization in this process using the `--seed` CLI option. - # Setting this allows you to use `--seed` to deterministically reproduce - # test failures related to randomization by passing the same `--seed` value - # as the one that triggered the failure. - Kernel.srand config.seed -=end end From 1f7fa6b31ced77adf3467d0d11f99f9960bb1777 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 17 Jul 2020 21:47:09 +0300 Subject: [PATCH 27/55] add unit testing for my_each_with_index --- lib/main.rb | 2 +- spec/main_spec.rb | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index e849b5d..71016bb 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -14,7 +14,7 @@ def my_each_with_index array = is_a?(Range) ? to_a : self index = -1 array.my_each { |item| yield(item, index += 1) } - array + self end def my_select diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 4a50094..c7711d1 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -1,7 +1,7 @@ require 'rspec' require '../lib/main' -Rspec.describe Enumerable do +RSpec.describe Enumerable do describe '#my_each' do it 'should be equal' do ary = %w[zero one two] @@ -26,4 +26,35 @@ expect(last).to eql(raw_range.last) end end + + describe '#my_each_with_index' do + it 'should be return array equal to the original' do + ary = %w[zero one two] + expect(ary.my_each_with_index { |value, key| key.to_s + value }).to eql(ary) + end + + it 'should be return range equal to the original' do + raw_range = (1..5) + expect(raw_range.my_each_with_index { |value| value }).to eql(raw_range) + end + + it 'should be excute the given block code' do + raw_ary = %w[one two three] + cap = '' + raw_ary.my_each_with_index do |item| + cap = item.upcase + end + expect(cap).to eql(raw_ary.last.upcase) + end + + it 'should work with range' do + raw_ary = %w[one two three] + last = 0 + raw_ary.my_each_with_index do |item, key| + last = key + item + end + expect(last).to eql(raw_ary.length - 1) + end + end end From a682e867d3cdf1edb573aa4a716d06ab9ce4e374 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 01:26:00 +0300 Subject: [PATCH 28/55] add unit testing for #my_select method --- lib/main.rb | 77 ++++++++++++++++++++++++++++++++++++++++------- spec/main_spec.rb | 32 +++++++++++++++----- 2 files changed, 90 insertions(+), 19 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 71016bb..c819837 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -26,28 +26,83 @@ def my_select filtered end - def my_all? + def my_all?(cond = nil) + array = is_a?(Range) ? to_a : self flag = true + if cond + if cond.is_a?(Class) + array.my_each do |item| + return false unless item.is_a?(cond) + end + else + array.my_each do |item| + return false unless item == cond + end + end + return flag + end + unless block_given? + array.my_each do |item| + return false unless item + end + return flag + end array.my_each do |item| - flag = yield(item) - break unless flag + return false unless yield(item) end flag end - def my_any? - flag = array.empty? - array.my_each do - break if yield(item) + def my_any?(cond = nil) + array = is_a?(Range) ? to_a : self + flag = false + if cond + if cond.is_a?(Class) + array.my_each do |item| + return true if item.is_a?(cond) + end + else + array.my_each do |item| + return true if item == cond + end + end + return flag + end + unless block_given? + array.my_each do |item| + return true if item + end + return flag + end + array.my_each do |item| + return true if yield(item) end flag end - def my_none? + def my_none?(cond = nil) + array = is_a?(Range) ? to_a : self flag = true - array.my_each do - flag = yield(item) - break if flag + if cond + if cond.is_a?(Class) + array.my_each do |item| + return false if item.is_a?(cond) + end + else + array.my_each do |item| + return false if item == cond + end + end + return flag + end + unless block_given? + array.my_each do |item| + return false if item + end + return flag + end + array.my_each do |item| + return false if yield(item) end flag end diff --git a/spec/main_spec.rb b/spec/main_spec.rb index c7711d1..602904f 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -2,6 +2,8 @@ require '../lib/main' RSpec.describe Enumerable do + fruits_ary = %w[apple banana strawberry pineapple] + describe '#my_each' do it 'should be equal' do ary = %w[zero one two] @@ -29,8 +31,7 @@ describe '#my_each_with_index' do it 'should be return array equal to the original' do - ary = %w[zero one two] - expect(ary.my_each_with_index { |value, key| key.to_s + value }).to eql(ary) + expect(fruits_ary.my_each_with_index { |value| value }).to eql(fruits_ary) end it 'should be return range equal to the original' do @@ -39,22 +40,37 @@ end it 'should be excute the given block code' do - raw_ary = %w[one two three] cap = '' - raw_ary.my_each_with_index do |item| + fruits_ary.my_each_with_index do |item| cap = item.upcase end - expect(cap).to eql(raw_ary.last.upcase) + expect(cap).to eql(fruits_ary.last.upcase) end it 'should work with range' do - raw_ary = %w[one two three] last = 0 - raw_ary.my_each_with_index do |item, key| + fruits_ary.my_each_with_index do |item, key| last = key item end - expect(last).to eql(raw_ary.length - 1) + expect(last).to eql(fruits_ary.length - 1) + end + end + + describe '#my_select' do + it 'should be return filtered array based on the given block' do + expect(fruits_ary.my_select { |value| value == 'banana' }).to eql(['banana']) + end + + it 'should work with range' do + filtered = (1..5).my_select do |value| + value if value == 1 + end + expect(filtered).to eql([1]) + end + + it 'should be return Enumerable if the block is missing' do + expect(fruits_ary.my_select.is_a?(Enumerable)).to eql(true) end end end From bfd70e26d33d4b9ea8870b9b39883ec79327876d Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 01:26:50 +0300 Subject: [PATCH 29/55] add unit testing for #my_all? method --- spec/main_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 602904f..afb1e44 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -73,4 +73,22 @@ expect(fruits_ary.my_select.is_a?(Enumerable)).to eql(true) end end + + describe '#my_all?' do + it 'should be return false based on the given block' do + expect(fruits_ary.my_all? { |value| value == 'banana' }).to eql(false) + end + + it 'should work with range' do + expect((1..5).my_all? { |val| val.is_a?(Numeric) }).to eql(true) + end + + it 'should return false if the given array has a falsy data' do + expect([nil, true, 99].my_all?).to eql(false) + end + + it 'should return true no falsy data is provided' do + expect([].my_all?).to eql(true) + end + end end From d45a9263716156bb261de16140ab61f7b3869cba Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 01:27:18 +0300 Subject: [PATCH 30/55] add unit testing for #my_any? method --- spec/main_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index afb1e44..0542b36 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -91,4 +91,31 @@ expect([].my_all?).to eql(true) end end + + describe '#my_any?' do + it 'should be return true based on the given block if it returns true' do + expect(fruits_ary.my_any? { |value| value == 'banana' }).to eql(true) + end + + it 'should work with range' do + expect((1..5).my_any? { |val| val.is_a?(String) }).to eql(false) + end + + it 'should return true if the given array has one truthy data' do + expect([nil, true, 99].my_any?).to eql(true) + end + + it 'should return true if the given type parameter match one of the element' do + ary = fruits_ary + [2] + expect(ary.my_any?(Numeric)).to eql(true) + end + + it 'should return true if the given parameter match one of the element' do + expect(fruits_ary.my_any?('banana')).to eql(true) + end + + it 'should return fasle no truthy data is provided' do + expect([].my_any?).to eql(false) + end + end end From bc1e98264fe87b8c681b348545ccda5f4d8c51e5 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 02:26:42 +0300 Subject: [PATCH 31/55] add unit test for the #my_none? --- lib/main.rb | 2 ++ spec/main_spec.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index c819837..1cb8356 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -125,6 +125,8 @@ def my_count(arg = nil) end def my_map(prok = nil) + return enum_for unless block_given? + array = is_a?(Range) ? to_a : self filtered = [] diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 0542b36..3040c3c 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -118,4 +118,22 @@ expect([].my_any?).to eql(false) end end + + describe '#my_none?' do + it 'should be return false based on the given block ' do + expect(fruits_ary.my_none? { |value| value == 'banana' }).to eql(false) + end + + it 'should work with range' do + expect((1..5).my_none? { |val| val.is_a?(String) }).to eql(true) + end + + it 'should return true if the given array has no truthy data' do + expect([nil, false].my_none?).to eql(true) + end + + it 'should return true no truthy data is provided' do + expect([].my_none?).to eql(true) + end + end end From 2dc478f7ea387984756bfaa5863eb36a618c093a Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 02:27:28 +0300 Subject: [PATCH 32/55] add unit test for the #my_count --- spec/main_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 3040c3c..d3de130 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -136,4 +136,22 @@ expect([].my_none?).to eql(true) end end + + describe '#my_count' do + it 'should be return the length of the array if no block given' do + expect(fruits_ary.my_count).to eql(fruits_ary.length) + end + + it 'should work with range' do + expect((1..5).my_count).to eql(5) + end + + it 'should return true if the given array has no truthy data' do + expect(fruits_ary.my_count(100)).to eql(fruits_ary.count(100)) + end + + it 'should return true no truthy data is provided' do + expect(fruits_ary.my_count { |item| item.eql?('banana') }).to eql(1) + end + end end From 409cbdede2dcb7112ee6275ac4c57e9909920f33 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 02:27:44 +0300 Subject: [PATCH 33/55] add unit test for the #my_map --- spec/main_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index d3de130..448d876 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -154,4 +154,18 @@ expect(fruits_ary.my_count { |item| item.eql?('banana') }).to eql(1) end end + + describe '#my_map' do + it 'should be return the filtered of the array based on the given block' do + expect(fruits_ary.my_map { |item| item.length * 100 }).to eql(fruits_ary.map { |item| item.length * 100 }) + end + + it 'should work with range' do + expect((1..5).my_map { |i| i * i }).to eql([1, 4, 9, 16, 25]) + end + + it 'should be return Enumerable if the block is missing' do + expect(fruits_ary.my_map.is_a?(Enumerable)).to eql(true) + end + end end From 0d62c0b90438a617ad488d416467c438e435b5f4 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 03:29:26 +0300 Subject: [PATCH 34/55] add unit test for the Enumanator method #my_inject --- lib/main.rb | 9 ++++--- spec/main_spec.rb | 62 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 1cb8356..3c3f57a 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -138,16 +138,18 @@ def my_map(prok = nil) filtered end - def my_inject(init = nil) + def my_inject(init = nil, symbol = nil) array = is_a?(Range) ? to_a : self acc = init - unless init.is_a?(Integer) + + if !init || init.is_a?(Symbol) acc = array[0] array.shift end - if init.is_a?(Symbol) + if init.is_a?(Symbol) || symbol.is_a?(Symbol) + init = symbol || init array.my_each do |item| acc = acc.send(init, item) end @@ -164,3 +166,4 @@ def multiply_els my_inject { |mul, n| mul * n } end end + diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 448d876..4f55ea7 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -6,8 +6,7 @@ describe '#my_each' do it 'should be equal' do - ary = %w[zero one two] - expect(ary.my_each(&:upcase)).to eql(ary) + expect(fruits_ary.my_each(&:upcase)).to eql(fruits_ary) end it 'should be excute the given block code' do @@ -30,11 +29,11 @@ end describe '#my_each_with_index' do - it 'should be return array equal to the original' do + it 'should return array equal to the original' do expect(fruits_ary.my_each_with_index { |value| value }).to eql(fruits_ary) end - it 'should be return range equal to the original' do + it 'should return range equal to the original' do raw_range = (1..5) expect(raw_range.my_each_with_index { |value| value }).to eql(raw_range) end @@ -58,7 +57,7 @@ end describe '#my_select' do - it 'should be return filtered array based on the given block' do + it 'should return filtered array based on the given block' do expect(fruits_ary.my_select { |value| value == 'banana' }).to eql(['banana']) end @@ -69,13 +68,13 @@ expect(filtered).to eql([1]) end - it 'should be return Enumerable if the block is missing' do + it 'should return Enumerable if the block is missing' do expect(fruits_ary.my_select.is_a?(Enumerable)).to eql(true) end end describe '#my_all?' do - it 'should be return false based on the given block' do + it 'should return false based on the given block' do expect(fruits_ary.my_all? { |value| value == 'banana' }).to eql(false) end @@ -93,7 +92,7 @@ end describe '#my_any?' do - it 'should be return true based on the given block if it returns true' do + it 'should return true based on the given block if it returns true' do expect(fruits_ary.my_any? { |value| value == 'banana' }).to eql(true) end @@ -120,7 +119,7 @@ end describe '#my_none?' do - it 'should be return false based on the given block ' do + it 'should return false based on the given block ' do expect(fruits_ary.my_none? { |value| value == 'banana' }).to eql(false) end @@ -138,7 +137,7 @@ end describe '#my_count' do - it 'should be return the length of the array if no block given' do + it 'should return the length of the array if no block given' do expect(fruits_ary.my_count).to eql(fruits_ary.length) end @@ -156,7 +155,7 @@ end describe '#my_map' do - it 'should be return the filtered of the array based on the given block' do + it 'should return the filtered of the array based on the given block' do expect(fruits_ary.my_map { |item| item.length * 100 }).to eql(fruits_ary.map { |item| item.length * 100 }) end @@ -164,8 +163,47 @@ expect((1..5).my_map { |i| i * i }).to eql([1, 4, 9, 16, 25]) end - it 'should be return Enumerable if the block is missing' do + it 'should return Enumerable if the block is missing' do expect(fruits_ary.my_map.is_a?(Enumerable)).to eql(true) end end + + describe '#my_inject' do + it 'should return the accumulated value' do + ary = [5, 6, 7, 8, 9, 10] + expect(ary.my_inject { |acc, n| acc + n }).to eql(45) + end + + it 'should work with range' do + expect((1..4).inject { |product, n| product * n }).to eql(24) + end + + it 'should return the accumulated value based the given parameter' do + expect((5..10).my_inject(100) { |acc, n| acc + n }).to eql(145) + end + + it 'should return the accumulated value based the given wild card' do + expect((1..4).my_inject(:+) { |acc, n| acc + n }).to eql(10) + end + + it 'should return the accumulated value based the given wild card and initial value' do + expect((1..4).my_inject(10, :*) { |acc, n| acc + n }).to eql(240) + end + + it 'should return the longest word' do + longest = %w[cat sheep bear].inject do |memo, word| + memo.length > word.length ? memo : word + end + expect(longest).to eql('sheep') + end + + it 'should return the longest wor' do + votes = ["Hayat", "Sky Light", "Hayat"] + result = votes.my_inject(Hash.new(0)) do |res, vote| + res[vote] = res[vote] + 1 + res + end + expect(result['Hayat']).to eql(2) + end + end end From 774893ed6b1cf1dd4765e50930614f9e287f1c28 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 03:30:52 +0300 Subject: [PATCH 35/55] fix main.rb linting issue --- lib/main.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 3c3f57a..a21a0e9 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -166,4 +166,3 @@ def multiply_els my_inject { |mul, n| mul * n } end end - From fd30ea4428d25cb47d1e0997d55caca0b04c8308 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 03:36:14 +0300 Subject: [PATCH 36/55] fix linting error spec files --- spec/main_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 4f55ea7..318b1eb 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -198,7 +198,7 @@ end it 'should return the longest wor' do - votes = ["Hayat", "Sky Light", "Hayat"] + votes = %w[Hayat Sky Light Hayat] result = votes.my_inject(Hash.new(0)) do |res, vote| res[vote] = res[vote] + 1 res From bd2de9671e2d54db2bb92de6fcb758900f6eb1d4 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 04:40:45 +0300 Subject: [PATCH 37/55] fix rubocop too many lines issue --- lib/main.rb | 93 +++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 56 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index a21a0e9..0ecf412 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,3 +1,22 @@ +def check_codition(flag, array = []) + if cond.is_a?(Class) + array.my_each do |item| + return !flag if item.is_a?(cond) + end + else + array.my_each do |item| + return !flag if item == cond + end + end + flag +end + +def check_block(flag, array) + array.my_each do |item| + return !flag if item + end + flag +end module Enumerable def my_each return enum_for unless block_given? @@ -29,24 +48,10 @@ def my_select def my_all?(cond = nil) array = is_a?(Range) ? to_a : self flag = true - if cond - if cond.is_a?(Class) - array.my_each do |item| - return false unless item.is_a?(cond) - end - else - array.my_each do |item| - return false unless item == cond - end - end - return flag - end - unless block_given? - array.my_each do |item| - return false unless item - end - return flag - end + return check_codition(flag, array) if cond + + return check_block(flag, array) unless block_given? + array.my_each do |item| return false unless yield(item) end @@ -56,24 +61,11 @@ def my_all?(cond = nil) def my_any?(cond = nil) array = is_a?(Range) ? to_a : self flag = false - if cond - if cond.is_a?(Class) - array.my_each do |item| - return true if item.is_a?(cond) - end - else - array.my_each do |item| - return true if item == cond - end - end - return flag - end - unless block_given? - array.my_each do |item| - return true if item - end - return flag - end + + return check_condition(flag, array) if cond + + return check_block(flag, array) unless block_given? + array.my_each do |item| return true if yield(item) end @@ -83,24 +75,11 @@ def my_any?(cond = nil) def my_none?(cond = nil) array = is_a?(Range) ? to_a : self flag = true - if cond - if cond.is_a?(Class) - array.my_each do |item| - return false if item.is_a?(cond) - end - else - array.my_each do |item| - return false if item == cond - end - end - return flag - end - unless block_given? - array.my_each do |item| - return false if item - end - return flag - end + + return check_codition(flag, array) if cond + + return check_block(flag, array) unless block_given? + array.my_each do |item| return false if yield(item) end @@ -162,7 +141,9 @@ def my_inject(init = nil, symbol = nil) acc end - def multiply_els - my_inject { |mul, n| mul * n } + def multiply_els(ary) + ary.my_inject do |acc, n| + acc * n + end end end From 012c6fe307df5b6230523d3d5945eab7614b232b Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 04:58:04 +0300 Subject: [PATCH 38/55] fix check_condition method --- .rubocop.yml | 1 + lib/main.rb | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 4e8ebd6..f6641eb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,6 @@ AllCops: Exclude: + - "README.md" - "Guardfile" - "Rakefile" diff --git a/lib/main.rb b/lib/main.rb index 0ecf412..0dd623a 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,4 +1,4 @@ -def check_codition(flag, array = []) +def check_condition(flag, array, cond) if cond.is_a?(Class) array.my_each do |item| return !flag if item.is_a?(cond) @@ -48,7 +48,7 @@ def my_select def my_all?(cond = nil) array = is_a?(Range) ? to_a : self flag = true - return check_codition(flag, array) if cond + return check_condition(flag, array, cond) if cond return check_block(flag, array) unless block_given? @@ -62,7 +62,7 @@ def my_any?(cond = nil) array = is_a?(Range) ? to_a : self flag = false - return check_condition(flag, array) if cond + return check_condition(flag, array, cond) if cond return check_block(flag, array) unless block_given? @@ -76,7 +76,7 @@ def my_none?(cond = nil) array = is_a?(Range) ? to_a : self flag = true - return check_codition(flag, array) if cond + return check_condition(flag, array, cond) if cond return check_block(flag, array) unless block_given? From 4db5fdcd7ff558d1b6073401cd508a8e5dec2226 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Sat, 18 Jul 2020 12:45:58 +0300 Subject: [PATCH 39/55] fix #my_inject complexity issue --- lib/main.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 0dd623a..e703803 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -17,6 +17,14 @@ def check_block(flag, array) end flag end + +def check_symbol(acc, init, symbol, array) + init = symbol || init + array.my_each do |item| + acc = acc.send(init, item) + end + acc +end module Enumerable def my_each return enum_for unless block_given? @@ -127,13 +135,7 @@ def my_inject(init = nil, symbol = nil) array.shift end - if init.is_a?(Symbol) || symbol.is_a?(Symbol) - init = symbol || init - array.my_each do |item| - acc = acc.send(init, item) - end - return acc - end + return check_symbol(acc, init, symbol, array) if init.is_a?(Symbol) || symbol.is_a?(Symbol) array.my_each do |item| acc = yield(acc, item) From 3e3f981e9607dcbb0720b9a0865edc65821da757 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Wed, 22 Jul 2020 14:34:09 +0300 Subject: [PATCH 40/55] fix #my_any? and #my_each bugs --- .idea/.gitignore | 8 ++++++++ .idea/misc.xml | 7 +++++++ .idea/modules.xml | 8 ++++++++ .idea/ruby-custom-enumerables.iml | 15 +++++++++++++++ .idea/vcs.xml | 6 ++++++ lib/main.rb | 10 +++++----- spec/main_spec.rb | 26 ++++++++++++++++++++++++-- 7 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/ruby-custom-enumerables.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..da55c2a --- /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..d310386 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/ruby-custom-enumerables.iml b/.idea/ruby-custom-enumerables.iml new file mode 100644 index 0000000..6e7c09c --- /dev/null +++ b/.idea/ruby-custom-enumerables.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ 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/lib/main.rb b/lib/main.rb index e703803..eccf770 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -32,7 +32,7 @@ def my_each array = is_a?(Range) ? to_a : self array.map { |item| yield(item) } - array + self end def my_each_with_index @@ -142,10 +142,10 @@ def my_inject(init = nil, symbol = nil) end acc end +end - def multiply_els(ary) - ary.my_inject do |acc, n| - acc * n - end +def multiply_els(ary) + ary.my_inject do |acc, n| + acc * n end end diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 318b1eb..998ebd3 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -5,11 +5,16 @@ fruits_ary = %w[apple banana strawberry pineapple] describe '#my_each' do - it 'should be equal' do + it 'should return array equal to the original' do expect(fruits_ary.my_each(&:upcase)).to eql(fruits_ary) end - it 'should be excute the given block code' do + it 'should return range equal to the original' do + rng = (1..5) + expect(rng.my_each { |item| item }).to eql(rng) + end + + it 'should be execute the given block code' do raw_ary = %w[one two three] cap = '' raw_ary.my_each do |item| @@ -82,10 +87,21 @@ expect((1..5).my_all? { |val| val.is_a?(Numeric) }).to eql(true) end + it 'should return true f all of the collection is a member of such class::Integer' do + expect((1..5).my_all? { |val| val.is_a?(Integer) }).to eql(true) + end + it 'should work with a class as an paramenter; return true false all items in the array are not a member String' do + expect(fruits_ary.my_all? { |val| val.is_a?(String) }).to eql(true) + end + it 'should return false if the given array has a falsy data' do expect([nil, true, 99].my_all?).to eql(false) end + it 'should return false if the given block return false; even once' do + expect([99, 100, 99].my_all? { |value| value == 99 }).to eql(false) + end + it 'should return true no falsy data is provided' do expect([].my_all?).to eql(true) end @@ -205,5 +221,11 @@ end expect(result['Hayat']).to eql(2) end + + it 'should not mutate the original value' do + ary = [2, 4, 8, 3] + ary.inject { |product, n| product * n } + expect(ary).to eql([2, 4, 8, 3]) + end end end From b394f6c2067c6e4b168f2b6319e331b85b62e7e2 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Wed, 22 Jul 2020 14:34:56 +0300 Subject: [PATCH 41/55] update readme to exclude .idea conf files --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e7ce268..7324fda 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,6 @@ build-iPhoneSimulator/ .history .vscode -.github \ No newline at end of file +.github + +.idea \ No newline at end of file From 1aa0aefe5de7ac5edcd0989018d8fbc59ad5266f Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 24 Jul 2020 04:04:41 +0300 Subject: [PATCH 42/55] fix regex issues for methods --- .rspec | 1 + lib/main.rb | 62 +++++++++++++++++++++++++++++++++++++++-------- spec/main_spec.rb | 27 ++++++++++++++++++--- 3 files changed, 77 insertions(+), 13 deletions(-) diff --git a/.rspec b/.rspec index c99d2e7..775c62b 100644 --- a/.rspec +++ b/.rspec @@ -1 +1,2 @@ --require spec_helper +--format documentation \ No newline at end of file diff --git a/lib/main.rb b/lib/main.rb index eccf770..be14d27 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,19 +1,56 @@ -def check_condition(flag, array, cond) - if cond.is_a?(Class) +def check_regex(flag, array, cond, neg) + if neg + array.my_each do |item| + return !flag unless cond.match(item.to_s) + end + else + array.my_each do |item| + return !flag if cond.match(item.to_s) + end + end + flag +end + +def check_class(flag, array, cond, neg) + if neg + array.my_each do |item| + return !flag unless item.is_a?(cond) + end + else array.my_each do |item| return !flag if item.is_a?(cond) end + end + flag +end + +def check_condition(flag, array, cond, neg = false) + return check_regex(flag, array, cond, neg) if cond.is_a?(Regexp) + + return check_class(flag, array, cond, neg) if cond.is_a?(Class) + + if neg + array.my_each do |item| + return !flag unless item == cond + end else array.my_each do |item| return !flag if item == cond end end + flag end -def check_block(flag, array) - array.my_each do |item| - return !flag if item +def check_block(flag, array, neg = false) + if neg + array.my_each do |item| + return !flag unless item + end + else + array.my_each do |item| + return !flag if item + end end flag end @@ -56,9 +93,9 @@ def my_select def my_all?(cond = nil) array = is_a?(Range) ? to_a : self flag = true - return check_condition(flag, array, cond) if cond + return check_condition(flag, array, cond, true) if cond - return check_block(flag, array) unless block_given? + return check_block(flag, array, true) unless block_given? array.my_each do |item| return false unless yield(item) @@ -126,13 +163,15 @@ def my_map(prok = nil) end def my_inject(init = nil, symbol = nil) - array = is_a?(Range) ? to_a : self + ary = is_a?(Range) ? to_a : self acc = init if !init || init.is_a?(Symbol) - acc = array[0] - array.shift + acc = ary[0] + array = ary.slice(1, ary.length - 1) + else + array = ary end return check_symbol(acc, init, symbol, array) if init.is_a?(Symbol) || symbol.is_a?(Symbol) @@ -149,3 +188,6 @@ def multiply_els(ary) acc * n end end + +ary = [5, 6, 7, 8, 9, 10] +p ary.my_inject(100) { |acc, val| acc + val } diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 998ebd3..d5af156 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -87,9 +87,18 @@ expect((1..5).my_all? { |val| val.is_a?(Numeric) }).to eql(true) end - it 'should return true f all of the collection is a member of such class::Integer' do - expect((1..5).my_all? { |val| val.is_a?(Integer) }).to eql(true) + it 'should return true if all of the collection match the given paramenter' do + expect([3, 3, 3, 3].my_all?(3)).to eql(true) end + + it 'should return true if all of the collection is a member of such class::Integer' do + expect([1, 2, 8, 6, 2, 0, 4, 7, 3].my_all? { |val| val.is_a?(Integer) }).to eql(true) + end + + it 'should return true if all of the collection is a member of such class::Integer #in shorter form' do + expect([6, 3, 3, 7, 1, 1, 2, 0, 0].my_all?(Integer)).to eql(true) + end + it 'should work with a class as an paramenter; return true false all items in the array are not a member String' do expect(fruits_ary.my_all? { |val| val.is_a?(String) }).to eql(true) end @@ -103,8 +112,12 @@ end it 'should return true no falsy data is provided' do - expect([].my_all?).to eql(true) + expect([1, true, 'hi', []].my_all?).to eql(true) end + + # it 'should return true ' do + # expect([1, true, 'hi', []].my_all?).to eql(true) + # end end describe '#my_any?' do @@ -116,6 +129,14 @@ expect((1..5).my_any? { |val| val.is_a?(String) }).to eql(false) end + it 'should work with regex and return false if the given paramenter not match the collection' do + expect(fruits_ary.my_any?(/z/)).to eql(false) + end + + it 'should work with regex and return true if the given paramenter match the collection' do + expect(fruits_ary.my_any?(/apple/)).to eql(true) + end + it 'should return true if the given array has one truthy data' do expect([nil, true, 99].my_any?).to eql(true) end From 2a6a97f8458daf838f9b90a51d4c209d7e304a96 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Fri, 24 Jul 2020 08:56:00 +0000 Subject: [PATCH 43/55] add .replit for repl.it configuration file --- .replit | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .replit diff --git a/.replit b/.replit new file mode 100644 index 0000000..f44b333 --- /dev/null +++ b/.replit @@ -0,0 +1,2 @@ +language = "ruby" +run = "lib/main.rb" \ No newline at end of file From 24421781e795e624db42b45cdba9b37c3d968ef3 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Thu, 30 Jul 2020 00:37:57 +0300 Subject: [PATCH 44/55] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8151ea0..3446d3b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# NEXT MEAL +# RUBY CUSTOM ENUMERABLES ![Hireable](https://cdn.rawgit.com/hiendv/hireable/master/styles/default/yes.svg) [![Run on Repl.it](https://repl.it/badge/github/wrakc/Bubble-Sort---Microverse)](https://repl.it/@aliabdulaziz/ruby-custom-enumerables) > The Enumerable module that gets mixed into the Array and Hash classes (among others) and provides you with lots of handy iterator methods. To prove that there's no magic to it, custom build of those methods. From 434bba05223e5eadd2413b20eac35b91c616cfa9 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 18:36:43 +0300 Subject: [PATCH 45/55] refactor main-spec.rb file --- spec/main_spec.rb | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index d5af156..03914db 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -2,9 +2,9 @@ require '../lib/main' RSpec.describe Enumerable do - fruits_ary = %w[apple banana strawberry pineapple] + let(:fruits_ary) { %w[apple banana strawberry pineapple] } - describe '#my_each' do + context '#my_each' do it 'should return array equal to the original' do expect(fruits_ary.my_each(&:upcase)).to eql(fruits_ary) end @@ -33,7 +33,7 @@ end end - describe '#my_each_with_index' do + context '#my_each_with_index' do it 'should return array equal to the original' do expect(fruits_ary.my_each_with_index { |value| value }).to eql(fruits_ary) end @@ -61,7 +61,7 @@ end end - describe '#my_select' do + context '#my_select' do it 'should return filtered array based on the given block' do expect(fruits_ary.my_select { |value| value == 'banana' }).to eql(['banana']) end @@ -78,7 +78,7 @@ end end - describe '#my_all?' do + context '#my_all?' do it 'should return false based on the given block' do expect(fruits_ary.my_all? { |value| value == 'banana' }).to eql(false) end @@ -115,12 +115,9 @@ expect([1, true, 'hi', []].my_all?).to eql(true) end - # it 'should return true ' do - # expect([1, true, 'hi', []].my_all?).to eql(true) - # end end - describe '#my_any?' do + context '#my_any?' do it 'should return true based on the given block if it returns true' do expect(fruits_ary.my_any? { |value| value == 'banana' }).to eql(true) end @@ -155,7 +152,7 @@ end end - describe '#my_none?' do + context '#my_none?' do it 'should return false based on the given block ' do expect(fruits_ary.my_none? { |value| value == 'banana' }).to eql(false) end @@ -173,7 +170,7 @@ end end - describe '#my_count' do + context '#my_count' do it 'should return the length of the array if no block given' do expect(fruits_ary.my_count).to eql(fruits_ary.length) end @@ -191,7 +188,7 @@ end end - describe '#my_map' do + context '#my_map' do it 'should return the filtered of the array based on the given block' do expect(fruits_ary.my_map { |item| item.length * 100 }).to eql(fruits_ary.map { |item| item.length * 100 }) end @@ -205,7 +202,7 @@ end end - describe '#my_inject' do + context '#my_inject' do it 'should return the accumulated value' do ary = [5, 6, 7, 8, 9, 10] expect(ary.my_inject { |acc, n| acc + n }).to eql(45) From f588bf2312f1f5c2c567b479cb2535948e139091 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 18:44:34 +0300 Subject: [PATCH 46/55] replace reptitive value by :raw_range variable --- spec/main_spec.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 03914db..90ebcbc 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -3,6 +3,7 @@ RSpec.describe Enumerable do let(:fruits_ary) { %w[apple banana strawberry pineapple] } + let(:raw_range) { (1..5) } context '#my_each' do it 'should return array equal to the original' do @@ -10,8 +11,7 @@ end it 'should return range equal to the original' do - rng = (1..5) - expect(rng.my_each { |item| item }).to eql(rng) + expect(raw_range.my_each { |item| item }).to eql(rng) end it 'should be execute the given block code' do @@ -24,7 +24,6 @@ end it 'should work with range' do - raw_range = (1..5) last = 0 raw_range.my_each do |item| last = item @@ -39,7 +38,7 @@ end it 'should return range equal to the original' do - raw_range = (1..5) + expect(raw_range.my_each_with_index { |value| value }).to eql(raw_range) end @@ -67,7 +66,7 @@ end it 'should work with range' do - filtered = (1..5).my_select do |value| + filtered = raw_range.my_select do |value| value if value == 1 end expect(filtered).to eql([1]) @@ -84,7 +83,7 @@ end it 'should work with range' do - expect((1..5).my_all? { |val| val.is_a?(Numeric) }).to eql(true) + expect(raw_range.my_all? { |val| val.is_a?(Numeric) }).to eql(true) end it 'should return true if all of the collection match the given paramenter' do @@ -123,7 +122,7 @@ end it 'should work with range' do - expect((1..5).my_any? { |val| val.is_a?(String) }).to eql(false) + expect(raw_range.my_any? { |val| val.is_a?(String) }).to eql(false) end it 'should work with regex and return false if the given paramenter not match the collection' do @@ -158,7 +157,7 @@ end it 'should work with range' do - expect((1..5).my_none? { |val| val.is_a?(String) }).to eql(true) + expect(raw_range.my_none? { |val| val.is_a?(String) }).to eql(true) end it 'should return true if the given array has no truthy data' do @@ -176,7 +175,7 @@ end it 'should work with range' do - expect((1..5).my_count).to eql(5) + expect(raw_range.my_count).to eql(5) end it 'should return true if the given array has no truthy data' do @@ -194,7 +193,7 @@ end it 'should work with range' do - expect((1..5).my_map { |i| i * i }).to eql([1, 4, 9, 16, 25]) + expect(raw_range.my_map { |i| i * i }).to eql([1, 4, 9, 16, 25]) end it 'should return Enumerable if the block is missing' do From 20140e655e52fee23cc4fddcb58966d6561a452a Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 22:16:44 +0300 Subject: [PATCH 47/55] add unit test for #multiply_els --- spec/main_spec.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 90ebcbc..2775fbb 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -1,9 +1,11 @@ require 'rspec' -require '../lib/main' +require_relative '../lib/main' RSpec.describe Enumerable do let(:fruits_ary) { %w[apple banana strawberry pineapple] } let(:raw_range) { (1..5) } + let(:str_ary) { %w[Hayat Sky Light Hayat] } + let(:number_ary) { [5, 6, 7, 8, 9, 10] } context '#my_each' do it 'should return array equal to the original' do @@ -15,12 +17,11 @@ end it 'should be execute the given block code' do - raw_ary = %w[one two three] cap = '' - raw_ary.my_each do |item| + str_ary.my_each do |item| cap = item.upcase end - expect(cap).to eql(raw_ary.last.upcase) + expect(cap).to eql(str_ary.last.upcase) end it 'should work with range' do @@ -224,7 +225,7 @@ end it 'should return the longest word' do - longest = %w[cat sheep bear].inject do |memo, word| + longest = %w[cat sheep bear] str_ary.inject do |memo, word| memo.length > word.length ? memo : word end expect(longest).to eql('sheep') @@ -245,4 +246,10 @@ expect(ary).to eql([2, 4, 8, 3]) end end + + context '#multiply_els' do + it 'should return the accumulated value' do + expect(multiply_els(number_ary)).to eql(45) + end + end end From e89071adbdfec820e0720675d6ad4401623f1f52 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 22:27:18 +0300 Subject: [PATCH 48/55] add unit test for #multiply_els --- spec/main_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 2775fbb..5c32f6d 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -204,8 +204,7 @@ context '#my_inject' do it 'should return the accumulated value' do - ary = [5, 6, 7, 8, 9, 10] - expect(ary.my_inject { |acc, n| acc + n }).to eql(45) + expect(number_ary.my_inject { |acc, n| acc + n }).to eql(45) end it 'should work with range' do From 7703ee7753b8a0ec5d0db30b485769fa2037f86f Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 20:01:37 +0000 Subject: [PATCH 49/55] fix #multiply_els testing method --- .replit | 2 +- lib/main.rb | 4 +--- spec/main_spec.rb | 10 +++++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.replit b/.replit index f44b333..4947139 100644 --- a/.replit +++ b/.replit @@ -1,2 +1,2 @@ language = "ruby" -run = "lib/main.rb" \ No newline at end of file +run = "rspec" \ No newline at end of file diff --git a/lib/main.rb b/lib/main.rb index be14d27..9fed2a0 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -62,6 +62,7 @@ def check_symbol(acc, init, symbol, array) end acc end + module Enumerable def my_each return enum_for unless block_given? @@ -188,6 +189,3 @@ def multiply_els(ary) acc * n end end - -ary = [5, 6, 7, 8, 9, 10] -p ary.my_inject(100) { |acc, val| acc + val } diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 5c32f6d..f7adc8d 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Enumerable do let(:fruits_ary) { %w[apple banana strawberry pineapple] } let(:raw_range) { (1..5) } - let(:str_ary) { %w[Hayat Sky Light Hayat] } + let(:str_ary) { %w[Hayat Sky-Light Hayat ] } let(:number_ary) { [5, 6, 7, 8, 9, 10] } context '#my_each' do @@ -13,7 +13,7 @@ end it 'should return range equal to the original' do - expect(raw_range.my_each { |item| item }).to eql(rng) + expect(raw_range.my_each { |item| item }).to eql(raw_range) end it 'should be execute the given block code' do @@ -224,10 +224,10 @@ end it 'should return the longest word' do - longest = %w[cat sheep bear] str_ary.inject do |memo, word| + longest = str_ary.inject do |memo, word| memo.length > word.length ? memo : word end - expect(longest).to eql('sheep') + expect(longest).to eql('Sky-Light') end it 'should return the longest wor' do @@ -248,7 +248,7 @@ context '#multiply_els' do it 'should return the accumulated value' do - expect(multiply_els(number_ary)).to eql(45) + expect(multiply_els(number_ary)).to eql(151200) end end end From a9266c5c587268b395d3e27eae42f9d575c616b8 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 23:38:31 +0300 Subject: [PATCH 50/55] update readme to add testing content --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3446d3b..b1a7ce9 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Set up your machine. - ruby - rubocop +- rspec ### Setup @@ -33,6 +34,8 @@ Set up your machine. > [download git](https://git-scm.com/downloads) - Follow the link below to install ruby > [Install Ruby](https://www.theodinproject.com/courses/ruby-programming/lessons/installing-ruby-ruby-programming) +- Follow the link below to set up rspec + > [Set up RSpec (testing tool) to run the tests](https://relishapp.com/rspec/docs/gettingstarted) - Follow the link below to set up a linter > [Set up Linter (rubocop) to check code errors](https://github.com/rubocop-hq/rubocop) - Clone the repository @@ -43,7 +46,10 @@ Set up your machine. ### Usage -Go to the project directory and run open the `ruby lib/ruby.rb` file on your terminal. +TO run unit tests + > Open your terminal then go to the project directory and execute `rspec` command +For running the program + > Open your terminal then go to the project directory and run the `ruby lib/main.rb` file on your terminal. ## Authors From 84902e460e358e45c6b7af18d75db0337c602a96 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 23:48:39 +0300 Subject: [PATCH 51/55] fix linting errors --- spec/main_spec.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index f7adc8d..e158941 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Enumerable do let(:fruits_ary) { %w[apple banana strawberry pineapple] } let(:raw_range) { (1..5) } - let(:str_ary) { %w[Hayat Sky-Light Hayat ] } + let(:str_ary) { %w[Hayat Sky-Light Hayat] } let(:number_ary) { [5, 6, 7, 8, 9, 10] } context '#my_each' do @@ -39,7 +39,6 @@ end it 'should return range equal to the original' do - expect(raw_range.my_each_with_index { |value| value }).to eql(raw_range) end @@ -114,7 +113,6 @@ it 'should return true no falsy data is provided' do expect([1, true, 'hi', []].my_all?).to eql(true) end - end context '#my_any?' do @@ -240,15 +238,14 @@ end it 'should not mutate the original value' do - ary = [2, 4, 8, 3] - ary.inject { |product, n| product * n } - expect(ary).to eql([2, 4, 8, 3]) + number_ary.inject { |product, n| product * n } + expect(ary).to eql([5, 6, 7, 8, 9, 10]) end end context '#multiply_els' do it 'should return the accumulated value' do - expect(multiply_els(number_ary)).to eql(151200) + expect(multiply_els(number_ary)).to eql(151_200) end end end From 77b2a27b403e2e839f0554e75b3f3300633804cb Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Wed, 5 Aug 2020 00:16:58 +0300 Subject: [PATCH 52/55] fix Metrics/BlockLength linting errors --- spec/main_spec.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index e158941..0831f9b 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -66,10 +66,7 @@ end it 'should work with range' do - filtered = raw_range.my_select do |value| - value if value == 1 - end - expect(filtered).to eql([1]) + expect(raw_range.my_select { |value| value if value == 1 }).to eql([1]) end it 'should return Enumerable if the block is missing' do @@ -91,11 +88,11 @@ end it 'should return true if all of the collection is a member of such class::Integer' do - expect([1, 2, 8, 6, 2, 0, 4, 7, 3].my_all? { |val| val.is_a?(Integer) }).to eql(true) + expect(number_ary.my_all? { |val| val.is_a?(Integer) }).to eql(true) end it 'should return true if all of the collection is a member of such class::Integer #in shorter form' do - expect([6, 3, 3, 7, 1, 1, 2, 0, 0].my_all?(Integer)).to eql(true) + expect(number_ary.my_all?(Integer)).to eql(true) end it 'should work with a class as an paramenter; return true false all items in the array are not a member String' do @@ -229,8 +226,7 @@ end it 'should return the longest wor' do - votes = %w[Hayat Sky Light Hayat] - result = votes.my_inject(Hash.new(0)) do |res, vote| + result = str_ary.my_inject(Hash.new(0)) do |res, vote| res[vote] = res[vote] + 1 res end @@ -238,8 +234,7 @@ end it 'should not mutate the original value' do - number_ary.inject { |product, n| product * n } - expect(ary).to eql([5, 6, 7, 8, 9, 10]) + expect(number_ary.inject { |product, n| product * n }).to eql([5, 6, 7, 8, 9, 10]) end end From fc02e7aa164246eedfcf0a07c1d3f6cbd7770699 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 21:32:03 +0000 Subject: [PATCH 53/55] fix linting errors on #my_inject --- spec/main_spec.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 0831f9b..26ddc7e 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -219,10 +219,7 @@ end it 'should return the longest word' do - longest = str_ary.inject do |memo, word| - memo.length > word.length ? memo : word - end - expect(longest).to eql('Sky-Light') + expect( str_ary.inject { |memo, word| memo.length > word.length ? memo : word } ).to eql('Sky-Light') end it 'should return the longest wor' do @@ -234,7 +231,8 @@ end it 'should not mutate the original value' do - expect(number_ary.inject { |product, n| product * n }).to eql([5, 6, 7, 8, 9, 10]) + number_ary.my_inject { |product, n| product * n } + expect(number_ary).to eql([5, 6, 7, 8, 9, 10]) end end From 174f7f30820f7a4ad9e9ae2726076cb12d4a9d48 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Tue, 4 Aug 2020 21:42:20 +0000 Subject: [PATCH 54/55] fix Layout/SpaceInsideParens error on '#my_inject longest word' test --- spec/main_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/main_spec.rb b/spec/main_spec.rb index 26ddc7e..2c74d1f 100644 --- a/spec/main_spec.rb +++ b/spec/main_spec.rb @@ -219,7 +219,7 @@ end it 'should return the longest word' do - expect( str_ary.inject { |memo, word| memo.length > word.length ? memo : word } ).to eql('Sky-Light') + expect(str_ary.inject { |memo, word| memo.length > word.length ? memo : word }).to eql('Sky-Light') end it 'should return the longest wor' do From 27567ff066a6a90758588eb4a39341f2a05498d8 Mon Sep 17 00:00:00 2001 From: Abdulaziz Ali Date: Wed, 2 Mar 2022 00:31:57 -0600 Subject: [PATCH 55/55] update readme. --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index b1a7ce9..35bbf87 100644 --- a/README.md +++ b/README.md @@ -71,9 +71,6 @@ Feel free to check the [issues page](issues/). Give a ⭐️ if you like this project! -## Acknowledgments - -- [Thanks for Microverse][https://github.com/microverseinc] ## 📝 License