├── .rubocop.yml ├── .stickler.yml ├── Gemfile ├── Gemfile.lock ├── README.md └── enumerables.rb /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | Exclude: 3 | - "README.md" 4 | - "Guardfile" 5 | - "Rakefile" 6 | 7 | DisplayCopNames: true 8 | 9 | Layout/LineLength: 10 | Max: 120 11 | Metrics/MethodLength: 12 | Max: 20 13 | Metrics/AbcSize: 14 | Max: 50 15 | Metrics/ClassLength: 16 | Max: 150 17 | Metrics/BlockLength: 18 | ExcludedMethods: ['describe'] 19 | Max: 30 20 | 21 | 22 | Style/Documentation: 23 | Enabled: false 24 | Style/ClassAndModuleChildren: 25 | Enabled: false 26 | Style/EachForSimpleLoop: 27 | Enabled: false 28 | Style/AndOr: 29 | Enabled: false 30 | Style/DefWithParentheses: 31 | Enabled: false 32 | Style/FrozenStringLiteralComment: 33 | EnforcedStyle: never 34 | 35 | Layout/HashAlignment: 36 | EnforcedColonStyle: key 37 | Layout/ExtraSpacing: 38 | AllowForAlignment: false 39 | Layout/MultilineMethodCallIndentation: 40 | Enabled: true 41 | EnforcedStyle: indented 42 | Lint/RaiseException: 43 | Enabled: false 44 | Lint/StructNewOverride: 45 | Enabled: false 46 | Style/HashEachMethods: 47 | Enabled: false 48 | Style/HashTransformKeys: 49 | Enabled: false 50 | Style/HashTransformValues: 51 | Enabled: false 52 | 53 | # rubocop:disable Style/CaseEquality -------------------------------------------------------------------------------- /.stickler.yml: -------------------------------------------------------------------------------- 1 | # add the linters you want stickler to use for this project 2 | linters: 3 | rubocop: 4 | display_cop_names: true 5 | # indicate where is the config file for stylelint 6 | config: "./rubocop.yml" 7 | 8 | files: 9 | ignore: 10 | - "Guardfile" 11 | - "Rakefile" 12 | - "node_modules/**/*" 13 | 14 | # PLEASE DO NOT enable auto fixing options 15 | # if you need extra support from you linter - do it in your local env as described in README for this config 16 | # find full documentation here: https://stickler-ci.com/docs -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rubocop' 4 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | ast (2.4.0) 5 | jaro_winkler (1.5.4) 6 | parallel (1.19.1) 7 | parser (2.7.1.1) 8 | ast (~> 2.4.0) 9 | rainbow (3.0.0) 10 | rexml (3.2.5) 11 | rubocop (0.82.0) 12 | jaro_winkler (~> 1.5.1) 13 | parallel (~> 1.10) 14 | parser (>= 2.7.0.1) 15 | rainbow (>= 2.2.2, < 4.0) 16 | rexml 17 | ruby-progressbar (~> 1.7) 18 | unicode-display_width (>= 1.4.0, < 2.0) 19 | ruby-progressbar (1.10.1) 20 | unicode-display_width (1.7.0) 21 | 22 | PLATFORMS 23 | ruby 24 | 25 | DEPENDENCIES 26 | rubocop 27 | 28 | BUNDLED WITH 29 | 2.1.4 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advanced Building Blocks - Enumerable Methods 2 | 3 | > This project consisted of recreating from scratch some of the Enumerable Methods present in Ruby and match the expected output with the original method (https://ruby-doc.org/core-2.7.0/Enumerable.html). The learning process of this project focused mainly on gaining knowledge on how to work with yield. 4 | 5 | The methods recreated were: 6 | 17 | 18 | 19 | As an additional requirement it was needed to: 20 | 24 | 25 | ## Screenshots 26 | 27 | ![screenshot](https://user-images.githubusercontent.com/52765379/80739904-4223ae80-8ae5-11ea-94a7-824f6e97582e.png) 28 | 29 | 30 | ## Live version 31 | 32 | [Live Version](https://repl.it/@ferbaco/Enumerables) 33 | 34 | ## Built With 35 | 36 | - Ruby 37 | - VSCode 38 | - Rubocop (Linter) with Stickler (CI Tool) 39 | 40 | ## Getting Started 41 | 42 | To get a local copy of the repository please run the following commands on your terminal: 43 | 44 | ``` 45 | $ cd 46 | ``` 47 | 48 | ``` 49 | $ git clone git@github.com:ferbaco86/enumerables-ruby.git 50 | ``` 51 | 52 | ## Author 53 | 54 | 👤 **Fernando Bahamondes** 55 | 56 | - Github: [@ferbaco86](https://github.com/ferbaco86) 57 | - Twitter: [@ferbac0](https://twitter.com/ferbac0) 58 | - Linkedin: [Fernando Bahamondes](https://www.linkedin.com/in/fernando-bahamondes-correa) 59 | 60 | ## 🤝 Contributing 61 | 62 | Contributions, issues and feature requests are welcome! 63 | 64 | Feel free to check the [issues page](https://github.com/ferbaco86/enumerables-ruby/issues). 65 | 66 | ## Show your support 67 | 68 | Give a ⭐️ if you like this project! 69 | 70 | ## Acknowledgments 71 | 72 | - Project inspired by Microverse Program. -------------------------------------------------------------------------------- /enumerables.rb: -------------------------------------------------------------------------------- 1 | # rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Style/CaseEquality 2 | module Enumerable 3 | def my_each 4 | return enum_for(:my_each) unless block_given? 5 | 6 | cont = 0 7 | arr = * self 8 | while arr.length > cont 9 | yield(arr[cont]) 10 | cont += 1 11 | end 12 | arr 13 | end 14 | 15 | def my_each_with_index 16 | return enum_for(:my_each_with_index) unless block_given? 17 | 18 | cont = 0 19 | my_each do |item| 20 | yield(item, cont) 21 | cont += 1 22 | end 23 | end 24 | 25 | def my_select 26 | return enum_for(:my_select) unless block_given? 27 | 28 | arr = [] 29 | my_each { |item| arr.push(item) if yield(item) } 30 | arr 31 | end 32 | 33 | def my_all?(args = nil) 34 | return true if empty? 35 | 36 | false_elements = [] 37 | my_each do |item| 38 | if block_given? 39 | false_elements.push(item) unless yield(item) 40 | elsif !args.nil? 41 | false_elements.push(item) unless args === item 42 | elsif !item || item.nil? 43 | false_elements.push(item) 44 | end 45 | end 46 | false_elements.empty? ? true : false 47 | end 48 | 49 | def my_any?(args = nil) 50 | return false if empty? 51 | 52 | true_elements = [] 53 | my_each do |item| 54 | if block_given? 55 | true_elements.push(item) if yield(item) 56 | elsif !args.nil? 57 | true_elements.push(item) if args === item 58 | elsif item 59 | true_elements.push(item) 60 | end 61 | end 62 | true_elements.empty? ? false : true 63 | end 64 | 65 | def my_none?(args = nil) 66 | return true if empty? 67 | 68 | true_elements = [] 69 | my_each do |item| 70 | if block_given? 71 | true_elements.push(item) if yield(item) 72 | elsif !args.nil? 73 | true_elements.push(item) if args === item 74 | elsif item 75 | true_elements.push(item) 76 | end 77 | end 78 | true_elements.empty? ? true : false 79 | end 80 | 81 | def my_count(args = nil) 82 | item_counter = 0 83 | my_each do |item| 84 | if !args.nil? 85 | item_counter += 1 if item == args 86 | elsif block_given? 87 | item_counter += 1 if yield(item) 88 | else 89 | item_counter += 1 90 | end 91 | end 92 | item_counter 93 | end 94 | 95 | def my_map(args = nil) 96 | return enum_for(:my_map) unless block_given? 97 | 98 | new_array = [] 99 | args.nil? ? my_each { |item| new_array.push(yield(item)) } : my_each { |item| new_array.push(args.call(item)) } 100 | new_array 101 | end 102 | 103 | def my_inject(*args) 104 | arr = *self 105 | 106 | if block_given? 107 | temp = args[0] if args.length == 1 108 | temp = arr.shift unless args.nil? || args.length == 1 109 | arr.my_each { |item| temp = yield(temp, item) } 110 | else 111 | temp = args.length > 1 ? args.shift : arr.shift 112 | arr.my_each { |item| temp = temp.send(args[0].to_s, item) } 113 | return temp 114 | end 115 | temp 116 | end 117 | end 118 | 119 | # rubocop:enable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Style/CaseEquality 120 | def multiply_els(args) 121 | puts args.my_inject(:*) 122 | end 123 | --------------------------------------------------------------------------------