From 56736e0be43603b835c944bcfd1b3ec8d7c7e57b Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Mon, 28 Oct 2019 18:24:47 -0700 Subject: [PATCH 01/11] rubocop and stickler deleted --- .rubocop.yml | 0 .stickler.yml | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .rubocop.yml delete mode 100644 .stickler.yml diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index e69de29..0000000 diff --git a/.stickler.yml b/.stickler.yml deleted file mode 100644 index e69de29..0000000 From c5039f2cd63316251b0878def852a06158d0ee15 Mon Sep 17 00:00:00 2001 From: Kingsley McSimon O <48096869+KingsleyMcSimon@users.noreply.github.com> Date: Mon, 28 Oct 2019 10:30:20 -0700 Subject: [PATCH 02/11] Create .rubocop.yml --- .rubocop.yml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..c0c7636 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,33 @@ +AllCops: + Exclude: + - "db/**/*" + - "config/**/*" + - "Guardfile" + - "Rakefile" + DisplayCopNames: true + +Metrics/LineLength: + Max: 120 +Metrics/MethodLength: + Include: ["app/controllers/*"] + Max: 20 +Metrics/AbcSize: + Include: ["app/controllers/*"] + Max: 30 +Metrics/ClassLength: + Max: 150 +Metrics/BlockLength: + ExcludedMethods: ['describe'] + +Style/Documentation: + Enabled: false +Style/ClassAndModuleChildren: + Enabled: false + +Layout/AlignHash: + EnforcedColonStyle: key +Layout/ExtraSpacing: + AllowForAlignment: false +Layout/MultilineMethodCallIndentation: + Enabled: true + EnforcedStyle: indented From faa17ec424069f3b8d75379d20d862f19b53d9e2 Mon Sep 17 00:00:00 2001 From: Kingsley McSimon O <48096869+KingsleyMcSimon@users.noreply.github.com> Date: Mon, 28 Oct 2019 10:31:44 -0700 Subject: [PATCH 03/11] Create .stickler.yml --- .stickler.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .stickler.yml diff --git a/.stickler.yml b/.stickler.yml new file mode 100644 index 0000000..2a4eb06 --- /dev/null +++ b/.stickler.yml @@ -0,0 +1,10 @@ +linters: + rubocop: + display_cop_names: true +files: + ignore: + - "bin/*" + - "db/*" + - "config/*" + - "Guardfile" + - "Rakefile" From df166e075c04d07a2bb30b57649e63869842a58b Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Mon, 28 Oct 2019 21:02:24 -0700 Subject: [PATCH 04/11] Descriptive Readme --- README.md | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f83871f..a3fff0f 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,84 @@ -# Project Descriptioon: +# Name of Project: -Advanced Building Blocks - Enumerable Methods Using Ruby Language. +Enumerable Methods + +# Table of contents: + +1. About The Project +2. Built With +3. Getting Started With The Project +4. Contributing +5. License +6. Contact/Contributors + i. Link To Project On Github +7. Acknowledgements + +# About The Project: + +Enumerable is a module in Ruby that provides many methods that can help with traversal, transforming, sorting, and filtering. Any class that can implement the #each method can use Enumerable methods, which will then yield each item in the collection to a block. + +# Built With: + +This Enumerable method project was built using the Ruby language. + +# Getting Started With The Project: + +To get started, here below are the steps that needs to be taken in order to achieve the required result from the project: + +1. Create a script file to house your methods and run it in IRB to test them later. +2. Add your new methods onto the existing Enumerable module. Ruby makes this easy for you because any class or module can be added to without trouble … just do something like: + + module Enumerable + def my_each + # your code here + end + end + +1. Create #my_each, a method that is identical to #each but (obviously) does not use #each. You’ll need to remember the yield statement. Make sure it returns the same thing as #each as well. +2. Create #my_each_with_index in the same way. +3. Create #my_select in the same way, though you may use #my_each in your definition (but not #each). +4. Create #my_all? (continue as above) +5. Create #my_any? +6. Create #my_none? +7. Create #my_count +8. Create #my_map +9. Create #my_inject +10. Test your #my_inject by creating a method called #multiply_els which multiplies all the elements of the array together by using #my_inject, e.g. multiply_els([2,4,5]) #=> 40 +11. Modify your #my_map method to take a proc instead. +12. Modify your #my_map method to take either a proc or a block. It won’t be necessary to apply both a proc and a block in the same #my_map call since you could get the same effect by chaining together one #my_map call with the block and one with the proc. This approach is also clearer, since the user doesn’t have to remember whether the proc or block will be run first. So if both a proc and a block are given, only execute the proc. + +# contributing: + +In this project, your contributions towards helping in improving this project is woild be well accomodated. With that, the project will get better rating and give programmers at any level the ability learn, be inspired and create better content. + +After you are done with your contributions, you can: + +i. Fork The project. +ii. Create your feature branch using git checkout -b feature/anyfeature(for example) +iii. Commit your changes following git commit -m 'Name it as it pleases you', when you are done, +iv. Push to the branch you have created using git push origin feature/anyfeature and finally +v. Open a pull request. + +# License: + +Distributed under the MIT Licence. See Licence for more informaton # Contributor(s): - -Kingsley McSimon Ogbonna https://github.com/KingsleyMcSimon @KingsleyMcSimon -# Link to the project on Github: +Kingsley McSimon Ogbonna @KingsleyMcSimon + +# Link To The Project On Github: https://github.com/KingsleyMcSimon/Enumerable-Methods +# Acknowledgements: + +https://medium.com/yello-offline/ruby-the-enumerable-module-under-the-hood-some-caveats-f640ce39a07d + +https://github.com/othneildrew/Best-README-Template + +https://www.theodinproject.com/courses/ruby-programming/lessons/advanced-building-blocks + + + From 88a65180dbe43d27f33b6ae1012fa51b16356bc4 Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Mon, 28 Oct 2019 21:33:42 -0700 Subject: [PATCH 05/11] Descriptive Readme Updated --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a3fff0f..bf3b241 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,15 @@ Enumerable Methods 5. License 6. Contact/Contributors i. Link To Project On Github -7. Acknowledgements +7. What Was Learnt During The Project +8. Acknowledgements # About The Project: Enumerable is a module in Ruby that provides many methods that can help with traversal, transforming, sorting, and filtering. Any class that can implement the #each method can use Enumerable methods, which will then yield each item in the collection to a block. +It can also be said that "Enumerable" is actually a "module", which means it is just a bunch of methods packaged together that can (and do) get "mixed in", or included, with other classes like Array and Hash. It's designed to provide useful functionality to classes representing collections of objects. + # Built With: This Enumerable method project was built using the Ruby language. @@ -71,6 +74,10 @@ Kingsley McSimon Ogbonna @KingsleyMcSimon https://github.com/KingsleyMcSimon/Enumerable-Methods +# What Was Learnt During The Project: + +I learnt from this project that modules like Enumerable are the ultimate in DRY (Don't Repeat Yourself) code. I also understood that Ruby programmers don't have to write all those methods as mentioned above many different times - they just write them once, package them up as Enumerables and tell Array and Hash to include them. + # Acknowledgements: https://medium.com/yello-offline/ruby-the-enumerable-module-under-the-hood-some-caveats-f640ce39a07d @@ -79,6 +86,7 @@ https://github.com/othneildrew/Best-README-Template https://www.theodinproject.com/courses/ruby-programming/lessons/advanced-building-blocks +https://www.vikingcodeschool.com/falling-in-love-with-ruby/the-enumerable-module From 1cd1d608957d568416d1fbe1dddf927bc4353977 Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Mon, 28 Oct 2019 22:04:44 -0700 Subject: [PATCH 06/11] my_inject method updated --- Enumerable_Methods.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Enumerable_Methods.rb b/Enumerable_Methods.rb index fee0ba6..83aec66 100644 --- a/Enumerable_Methods.rb +++ b/Enumerable_Methods.rb @@ -114,14 +114,14 @@ def my_map #my_map method mymap end - def my_map_again(myproc) #my_mapa_gain method which takes a proc + def my_map_again(myproc) #my_map_again method which takes a proc self.my_each do |b| mymapagain.push myproc.call(b) end mymapagain end - def my_map_final myproc = nil #my_map_final method which an either take a proc or a block + def my_map_final myproc = nil #my_map_final method which can either take a proc or a block mymapfinal = [] self.my_each do |b| if myproc == nil @@ -133,12 +133,13 @@ def my_map_final myproc = nil #my_map_final method which an either take a proc o mymapfinal end - def my_inject #my_inject method - output = nil - my_each = output ? yield (output, b) : self |0| + def my_inject(param = self[0]) #my_inject method + help = self + help[1..help.length].each do |b| + param = yield(param, b) end - output -end + param +end def multiply_els #multiply_els method my_inject {|mult, b | mult + b} From 32ba07bde651a3fba65fd504f5805f18369fbeb8 Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Wed, 30 Oct 2019 18:33:49 -0700 Subject: [PATCH 07/11] my_count method rewritted --- Enumerable_Methods.rb | 44 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/Enumerable_Methods.rb b/Enumerable_Methods.rb index 83aec66..5824d43 100644 --- a/Enumerable_Methods.rb +++ b/Enumerable_Methods.rb @@ -10,9 +10,7 @@ def my_each #This is my_each method self.size.times do yield(self[a]) a += 1 - end - self end @@ -22,9 +20,7 @@ def my_each_with_index #my_each_with_index yield(self[a], a) a += 1 end - self - end def my_select #my_select method @@ -32,13 +28,9 @@ def my_select #my_select method self.my_each do |b| if yield (b) myselect.push (b) - end - end - myselect - end def my_all? #my_all method @@ -63,14 +55,12 @@ def my_any? #my_any? method if yield (b) output = true break - end - end output end - def my_none? #my_none methods + def my_none? #my_none method output = true self.my_each do |b| if yield (b) @@ -81,30 +71,18 @@ def my_none? #my_none methods output end - def my_count parameter = nil #my_count method + def my_count #my_count method mycount = 0 - self.my_each do |b| - if parameter != nil - if parameter == self[b] - mycount += 1 - end - else - if yield (b) + myhelp = self + myhelp.my_each do |a| + if block_given? + mycount += 1 if yield(a) mycount += 1 - self.my_each do |element| - if parameter - if element == parameter - mycount += 1 - end - end - elsif block_given? - mycount += if yield(element) - else - mycount = self.length - end - end - mycount - end + end + end + mycount + end + def my_map #my_map method mymap = [] From 4bc4053504fc186ae069dd01217ea25141053646 Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Mon, 4 Nov 2019 22:36:34 -0800 Subject: [PATCH 08/11] Enumerable updates --- Enumerable_Methods.rb | 57 ++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/Enumerable_Methods.rb b/Enumerable_Methods.rb index 5824d43..e007c2b 100644 --- a/Enumerable_Methods.rb +++ b/Enumerable_Methods.rb @@ -5,38 +5,48 @@ module Enumerable - def my_each #This is my_each method - a = 0 - self.size.times do - yield(self[a]) - a += 1 + def my_each #my_each method + myeach = 0 + while myeach < size + yield self[myeach] + + myeach += 1 end + self end +end - def my_each_with_index #my_each_with_index - a = 0 - self.size.times do - yield(self[a], a) - a += 1 - end - self + +def my_each_with_index #my_each_with_index method + myeachwithindex = 0 + while myeachwithindex < size + yield (self[myeachwithindex], myeachwithindex) + + myeachwithindex += 1 end - def my_select #my_select method - myselect = [] - self.my_each do |b| - if yield (b) - myselect.push (b) - end + self +end +end + +def my_select #my_select method + myselect = [] + while myselect < size + if yield (b) + myselect.push (b) end myselect end +end + def my_all? #my_all method - myall = [] + myall = 0 + #myall = [] output = true - self.my_each do |b| + #self.my_each do |b| + while myall < size if yield (b) myall.push (b) end @@ -48,6 +58,7 @@ def my_all? #my_all method end output end +end def my_any? #my_any? method output = false @@ -208,3 +219,9 @@ def multiply_els #multiply_els method #puts multiply_els(arraysofnumbers) + +puts 'my_each method imp -> ' +[8, 2, 5, 4].my_each { |num| puts num } + + + From 9ed0c74e37d383795e3bd50c3b33562dfbd63837 Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Tue, 5 Nov 2019 14:49:50 -0800 Subject: [PATCH 09/11] Update in the methods --- Enumerable_Methods.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/Enumerable_Methods.rb b/Enumerable_Methods.rb index e007c2b..0c46a67 100644 --- a/Enumerable_Methods.rb +++ b/Enumerable_Methods.rb @@ -25,7 +25,6 @@ def my_each_with_index #my_each_with_index method myeachwithindex += 1 end - self end end From 85c2c8da2f27f47eb77615572e17f7e01ef2c7d8 Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Tue, 5 Nov 2019 23:55:34 -0800 Subject: [PATCH 10/11] Enumerable updates --- Enumerable_Methods.rb | 149 +++++++++++++----------------------------- 1 file changed, 46 insertions(+), 103 deletions(-) diff --git a/Enumerable_Methods.rb b/Enumerable_Methods.rb index 0c46a67..9a9f617 100644 --- a/Enumerable_Methods.rb +++ b/Enumerable_Methods.rb @@ -29,6 +29,7 @@ def my_each_with_index #my_each_with_index method end end + def my_select #my_select method myselect = [] while myselect < size @@ -40,47 +41,28 @@ def my_select #my_select method end - def my_all? #my_all method - myall = 0 - #myall = [] - output = true - #self.my_each do |b| - while myall < size - if yield (b) - myall.push (b) - end - end - if myall.length == self.length - output = true - else - output = false - end - output + def my_all? #my_all? method + myall = 0 + myall.my_each do |d| + return false unless yield(d) end + true end - def my_any? #my_any? method - output = false - self.my_each do |b| - if yield (b) - output = true - break - end - end - output + + def my_any #my_any method + self.my_each { |n| return true if yield (n) } end def my_none? #my_none method - output = true + mynone = true self.my_each do |b| - if yield (b) - output = false - break - end + return false if yield(b) end - output + true end + def my_count #my_count method mycount = 0 myhelp = self @@ -94,13 +76,25 @@ def my_count #my_count method end - def my_map #my_map method + def my_map(&proc) #my_map method + return self.to_enum unless block_given? + + mymap = [] - self.my_each do |b| - mymap.push yield(b) + if self.class == Hash + self.each do |a, b| + + mymap << proc.call(a, b) + end + mymap + else + self.my_each do |c| + mymap << proc.call(c) end mymap end +end +end def my_map_again(myproc) #my_map_again method which takes a proc self.my_each do |b| @@ -109,6 +103,7 @@ def my_map_again(myproc) #my_map_again method which takes a proc mymapagain end + def my_map_final myproc = nil #my_map_final method which can either take a proc or a block mymapfinal = [] self.my_each do |b| @@ -121,6 +116,8 @@ def my_map_final myproc = nil #my_map_final method which can either take a proc mymapfinal end + + def my_inject(param = self[0]) #my_inject method help = self help[1..help.length].each do |b| @@ -129,6 +126,8 @@ def my_inject(param = self[0]) #my_inject method param end + + def multiply_els #multiply_els method my_inject {|mult, b | mult + b} @@ -137,90 +136,34 @@ def multiply_els #multiply_els method - +#Here are examples that could be used to test for some of the methods above. #Testing for my_each method #(To test each of these methods, you can take it out of the comment and go ahead with the testing ) -#arraysofnumbers = [2, 4, 6, 8, 10, 12] -#arraysofnumbers.my_each do |numbers| - # numbers *= 5 - # print "#{numbers}" +#k = [1, 2, 3, 4, 5] +#k.my_each do |myeach| +#puts myeach #end #Testing for my_each_with_index method -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 40] -#arraysofnumbers.my_each_with_index do |value, index| -#puts "arraysofnumbers index #{index} takes the value of #{value}" +#fruits = %w(Mango Pawpaw Cashew Pear Orange) +#fruits.my_each_with_index do |myeachwithindex, idx| +#puts "#{myeachwithindex} is the fruit with index #{idx}" #end #Testing for my_select method -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 50] -#output = arraysofnumbers.my_select do |numbers| numbers % 2 != 0; end -#print output - -#Testing for my_all method - -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 40] -#output = arraysofnumbers.my_all? do |b| b % 2 == 0; end -#print output - -#Testing for my_any method - -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 25] -#output = arraysofnumbers.my_any? do |b| b < 3; end -#print output - -#Testing for my_none method - -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 10] -#output = arraysofnumbers.my_none? do |b| b % 2 == 1; end -#print output - -#Testing for my_count method - -#arraysofnumbers = [2, 5, 8, 10, 20, 15, 25, 15, 8, 4, 6] -#arraysofnumbers = [5, 8, 10, 15, 15, 8, 4, 6, 8, 2, 12, 7, 8, 10] -#output = arraysofnumbers.my_count -#output = arraysofnumbers.my_count (10) -#output = arraysofnumbers () do |numbers| numbers % 2 == 1; end -#output = arraysofnumbers (5) do |numbers|; end -#print output - -#Testing for my_map method - -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 20] -#output = arraysofumbers.my_map do |numbers| numbers * 2; end -#print output - -#Testing for my_map_again method (that takes a proc) - -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 10, 10] -#myproc = Proc.new do |numbers| numbers * 2; end -#output = arraysofnumbers.my_map_again(myproc) -#print output - -#Testing for my_map_final(3) (that takes either a proc or a block) - -#arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 35, 30] -#myproc = Proc.new do |numbers| numbers * 2; end -#output = arraysofnumbers.my_map_final do |numbers| numbers * 3; end -#print output - -#Testing for my_inject method - -#def multiply_els(arraysofnumbers) -#arraysofnumbers.my_inject(1) do |total, numbers|total * numbers; end +#strings = %w(Angle Ball Cat Capstone Conquer) +#puts strings.my_select {|word| word.size > 3 } #end -#arraysofnumbers = [1, 4, 5, 3, 2] -#puts multiply_els(arraysofnumbers) - -puts 'my_each method imp -> ' -[8, 2, 5, 4].my_each { |num| puts num } +#To test for my_inject +#array = [1, 2, 3, 4, 5] +#array.my_inject +#end From 34c23fd56df531080d7077fc418bea5d17a852ff Mon Sep 17 00:00:00 2001 From: kingsley McSimon Ogbonna Date: Wed, 6 Nov 2019 18:06:21 -0800 Subject: [PATCH 11/11] Another Enumerable updates --- Enumerable_Methods.rb | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/Enumerable_Methods.rb b/Enumerable_Methods.rb index 9a9f617..e52ccac 100644 --- a/Enumerable_Methods.rb +++ b/Enumerable_Methods.rb @@ -19,15 +19,13 @@ def my_each #my_each method def my_each_with_index #my_each_with_index method - myeachwithindex = 0 - while myeachwithindex < size - yield (self[myeachwithindex], myeachwithindex) - - myeachwithindex += 1 + k = 0 + while k < size + yield(self[k], k) + k += 1 end self end -end def my_select #my_select method @@ -94,30 +92,8 @@ def my_map(&proc) #my_map method mymap end end -end - - def my_map_again(myproc) #my_map_again method which takes a proc - self.my_each do |b| - mymapagain.push myproc.call(b) - end - mymapagain - end - - - def my_map_final myproc = nil #my_map_final method which can either take a proc or a block - mymapfinal = [] - self.my_each do |b| - if myproc == nil - mymapfinal.push yield(b) - else - mymapfinal.push myproc.call(b) - end - end - mymapfinal - end - - + def my_inject(param = self[0]) #my_inject method help = self help[1..help.length].each do |b|