├── .github └── workflows │ ├── linters.yml │ └── test.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── .vscode └── launch.json ├── LICENSE.md ├── README.md ├── enumerable.rb └── spec ├── spec_helper.rb └── test_spec.rb /.github/workflows/linters.yml: -------------------------------------------------------------------------------- 1 | name: Linters 2 | 3 | on: pull_request 4 | 5 | jobs: 6 | rubocop: 7 | name: Rubocop 8 | runs-on: ubuntu-18.04 9 | steps: 10 | - uses: actions/checkout@v2 11 | - uses: actions/setup-ruby@v1 12 | with: 13 | ruby-version: 2.6.x 14 | - name: Setup Rubocop 15 | run: | 16 | gem install --no-document rubocop:'~>0.81.0' # https://docs.rubocop.org/en/stable/installation/ 17 | [ -f .rubocop.yml ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ruby/.rubocop.yml 18 | - name: Rubocop Report 19 | run: rubocop --color 20 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: pull_request 4 | 5 | jobs: 6 | rspec: 7 | name: RSpec 8 | runs-on: ubuntu-18.04 9 | steps: 10 | - uses: actions/checkout@v2 11 | - uses: actions/setup-ruby@v1 12 | with: 13 | ruby-version: 2.6.x 14 | - name: Setup RSpec 15 | run: | 16 | [ -f Gemfile ] && bundle --deployment 17 | gem install --no-document rspec:'~>3.0' 18 | - name: RSpec Report 19 | run: rspec --force-color --format documentation -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | test_file.txt -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | --format documentation -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | Exclude: 3 | - "Guardfile" 4 | - "Rakefile" 5 | 6 | DisplayCopNames: true 7 | 8 | Layout/LineLength: 9 | Max: 120 10 | Metrics/MethodLength: 11 | Max: 20 12 | Metrics/AbcSize: 13 | Max: 50 14 | Metrics/ClassLength: 15 | Max: 150 16 | Metrics/BlockLength: 17 | ExcludedMethods: ['describe'] 18 | Max: 30 19 | 20 | 21 | Style/Documentation: 22 | Enabled: false 23 | Style/ClassAndModuleChildren: 24 | Enabled: false 25 | Style/EachForSimpleLoop: 26 | Enabled: false 27 | Style/AndOr: 28 | Enabled: false 29 | Style/DefWithParentheses: 30 | Enabled: false 31 | Style/FrozenStringLiteralComment: 32 | EnforcedStyle: never 33 | 34 | Layout/HashAlignment: 35 | EnforcedColonStyle: key 36 | Layout/ExtraSpacing: 37 | AllowForAlignment: false 38 | Layout/MultilineMethodCallIndentation: 39 | Enabled: true 40 | EnforcedStyle: indented 41 | Lint/RaiseException: 42 | Enabled: false 43 | Lint/StructNewOverride: 44 | Enabled: false 45 | Style/HashEachMethods: 46 | Enabled: false 47 | Style/HashTransformKeys: 48 | Enabled: false 49 | Style/HashTransformValues: 50 | Enabled: false -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Listen for rdebug-ide", 6 | "type": "Ruby", 7 | "request": "attach", 8 | "remoteHost": "127.0.0.1", 9 | "remotePort": "1234", 10 | "remoteWorkspaceRoot": "${workspaceRoot}" 11 | }, 12 | 13 | { 14 | "name": "Debug Local File", 15 | "type": "Ruby", 16 | "request": "launch", 17 | "cwd": "${workspaceRoot}", 18 | "program": "${file}", 19 | "programArgs": [], 20 | "showDebuggerOutput": true, 21 | "debuggerPort": "1234" 22 | } 23 | ], 24 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Elías Castañeda, Jessica Farias 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Enumerables 2 | 3 | The goal of this project was to duplicate some Enumerable class methods in order to learn about how to create methods inside classes. 4 | 5 | ## Output 6 | ```ruby 7 | Input 8 | 9 | my_numbers = [5 ,6, 7, 8] 10 | my_numbers.my_inject {|sum, number| sum + number} 11 | 12 | Output 13 | 14 | > 26 15 | ``` 16 | 17 | ## Demo Link 18 | 19 | [Live Demo Link](https://repl.it/@EliasCastaneda/Enumerables). 20 | 21 | ## Built With 22 | 23 | - Ruby 24 | - Rubocop 25 | 26 | ## Authors 27 | 28 | 👤 **Elias Castañeda** 29 | 30 | - Github: [@e71az](https://github.com/e71az) 31 | - Twitter: [@e71az](https://twitter.com/e71az) 32 | - Linkedin: [linkedin](https://www.linkedin.com/in/elias-casta%C3%B1eda-17a771115/) 33 | 34 | 👤 **Jessica Farias** 35 | 36 | - Github: [jessicafarias](https://github.com/jessicafarias) 37 | - Twitter: [@FariasRosado](https://twitter.com/FariasRosado) 38 | - Linkedin: [jessica-michelle-farias-rosado](https://www.linkedin.com/in/jessica-michelle-farias-rosado/) 39 | 40 | ## Getting Started 41 | 42 | To get a local copy up and running follow these simple example steps. 43 | 44 | 1. Open the terminal comand line window 45 | 2. `git clone https://github.com/e71az/Enumerables.git` 46 | 3. Open the spec folder and execute the bubble_spec.rb script 47 | 4. Enjoy! 48 | 49 | ## 🤝 Contributing 50 | 51 | Contributions, issues and feature requests are welcome! 52 | 53 | Feel free to check the [Issue page](https://github.com/e71az/Enumerables/issues). 54 | 55 | ## Show your support 56 | 57 | Give a ⭐️ if you like this project! 58 | 59 | ## 📝 License 60 | 61 | This project is [MIT](LICENSE) licensed. 62 | 63 | -------------------------------------------------------------------------------- /enumerable.rb: -------------------------------------------------------------------------------- 1 | # rubocop:disable Metrics/ModuleLength 2 | # rubocop:disable Metrics/CyclomaticComplexity 3 | # rubocop:disable Metrics/PerceivedComplexity 4 | module Enumerable 5 | def my_each 6 | return to_enum unless block_given? 7 | 8 | array = to_a 9 | array.length.times { |element| yield(array[element]) } 10 | self 11 | end 12 | 13 | def my_each_with_index 14 | return to_enum unless block_given? 15 | 16 | array = to_a 17 | array.length.times { |element| yield(array[element], element) } 18 | self 19 | end 20 | 21 | def my_select 22 | return to_enum unless block_given? 23 | 24 | array = [] 25 | my_each { |element| array.push(element) if yield(element) } 26 | array 27 | end 28 | 29 | def my_all?(*arguments) 30 | return "`my_all?': wrong # of arguments (given #{arguments.length}, expected 0..1)" if arguments.length > 1 31 | 32 | if block_given? 33 | my_each { |element| return false unless yield(element) } 34 | 35 | elsif arguments[0].is_a? Class 36 | my_each { |element| return false unless element.class.ancestors.include?(arguments[0]) } 37 | elsif arguments[0].is_a? Regexp 38 | my_each { |element| return false unless arguments[0].match(element) } 39 | elsif arguments.empty? 40 | return include?(nil) || include?(false) ? false : true 41 | else 42 | my_each { |element| return false unless element == arguments[0] } 43 | end 44 | true 45 | end 46 | 47 | def my_any?(*arguments) 48 | return "`my_any?': wrong number of arguments (given #{arguments.length}, expected 0..1)" if arguments.length > 1 49 | 50 | if block_given? 51 | my_each { |element| return true if yield(element) } 52 | elsif arguments.empty? 53 | my_each { |element| return true if element } 54 | return false 55 | elsif arguments[0].is_a? Class 56 | my_each { |element| return true if element.class.ancestors.include?(arguments[0]) } 57 | elsif arguments[0].is_a? Regexp 58 | my_each { |element| return true if arguments[0].match(element) } 59 | else 60 | my_each { |element| return true if element == arguments[0] } 61 | end 62 | false 63 | end 64 | 65 | def my_none?(*arguments) 66 | return "`my_none?': wrong number of arguments (given #{arguments.length}, expected 0..1)" if arguments.length > 1 67 | 68 | if block_given? 69 | my_each { |element| return false if yield(element) } 70 | elsif arguments.empty? 71 | my_each { |element| return false unless element.nil? || element == false } 72 | elsif arguments[0].is_a? Class 73 | my_each { |element| return false if element.class.ancestors.include?(arguments[0]) } 74 | elsif arguments[0].is_a? Regexp 75 | my_each { |element| return false if arguments[0].match(element) } 76 | else 77 | my_each { |element| return false if element == arguments[0] } 78 | end 79 | true 80 | end 81 | 82 | def my_count(*arguments) 83 | counter = 0 84 | if block_given? 85 | my_each { |element| counter += 1 if yield(element) } 86 | elsif arguments.empty? 87 | counter = to_a.length 88 | else 89 | to_a.my_each { |element| counter += 1 if element == arguments[0] } 90 | end 91 | return "`my_count?': wrong # of arguments (given #{arguments.length}, expected 0..1)" if arguments.length > 1 92 | 93 | counter 94 | end 95 | 96 | def my_map(proc = nil) 97 | arr = [] 98 | return to_enum unless block_given? 99 | 100 | if proc 101 | my_each { |element| arr << proc.call(element) } 102 | else 103 | my_each { |element| arr << yield(element) } 104 | end 105 | arr 106 | end 107 | 108 | def my_inject(number = nil, sym = nil) 109 | if block_given? 110 | accumulator = number 111 | my_each { |element| accumulator = accumulator.nil? ? element : yield(accumulator, element) } 112 | raise LocalJumpError unless block_given? || !sym.empty? || !number.empty? 113 | 114 | accumulator 115 | elsif !number.nil? && (number.is_a?(Symbol) || number.is_a?(String)) 116 | raise LocalJumpError unless block_given? || !number.empty? 117 | 118 | accumulator = nil 119 | my_each { |element| accumulator = accumulator.nil? ? element : accumulator.send(number, element) } 120 | accumulator 121 | elsif !sym.nil? && (sym.is_a?(Symbol) || sym.is_a?(String)) 122 | raise LocalJumpError unless block_given? || !sym.empty? 123 | 124 | accumulator = number 125 | my_each { |element| accumulator = accumulator.nil? ? element : accumulator.send(sym, element) } 126 | accumulator 127 | else 128 | raise LocalJumpError 129 | end 130 | end 131 | end 132 | # rubocop:enable Metrics/ModuleLength 133 | # rubocop:enable Metrics/CyclomaticComplexity 134 | # rubocop:enable Metrics/PerceivedComplexity 135 | def multiply_els(array) 136 | array.my_inject(:*) 137 | end 138 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16 | RSpec.configure do |config| 17 | # rspec-expectations config goes here. You can use an alternate 18 | # assertion/expectation library such as wrong or the stdlib/minitest 19 | # assertions if you prefer. 20 | config.expect_with :rspec do |expectations| 21 | # This option will default to `true` in RSpec 4. It makes the `description` 22 | # and `failure_message` of custom matchers include text for helper methods 23 | # defined using `chain`, e.g.: 24 | # be_bigger_than(2).and_smaller_than(4).description 25 | # # => "be bigger than 2 and smaller than 4" 26 | # ...rather than: 27 | # # => "be bigger than 2" 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | # rspec-mocks config goes here. You can use an alternate test double 32 | # library (such as bogus or mocha) by changing the `mock_with` option here. 33 | config.mock_with :rspec do |mocks| 34 | # Prevents you from mocking or stubbing a method that does not exist on 35 | # a real object. This is generally recommended, and will default to 36 | # `true` in RSpec 4. 37 | mocks.verify_partial_doubles = true 38 | end 39 | 40 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 41 | # have no way to turn it off -- the option exists only for backwards 42 | # compatibility in RSpec 3). It causes shared context metadata to be 43 | # inherited by the metadata hash of host groups and examples, rather than 44 | # triggering implicit auto-inclusion in groups with matching metadata. 45 | config.shared_context_metadata_behavior = :apply_to_host_groups 46 | 47 | # The settings below are suggested to provide a good initial experience 48 | # with RSpec, but feel free to customize to your heart's content. 49 | # # This allows you to limit a spec run to individual examples or groups 50 | # # you care about by tagging them with `:focus` metadata. When nothing 51 | # # is tagged with `:focus`, all examples get run. RSpec also provides 52 | # # aliases for `it`, `describe`, and `context` that include `:focus` 53 | # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 54 | # config.filter_run_when_matching :focus 55 | # 56 | # # Allows RSpec to persist some state between runs in order to support 57 | # # the `--only-failures` and `--next-failure` CLI options. We recommend 58 | # # you configure your source control system to ignore this file. 59 | # config.example_status_persistence_file_path = "spec/examples.txt" 60 | # 61 | # # Limits the available syntax to the non-monkey patched syntax that is 62 | # # recommended. For more details, see: 63 | # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 64 | # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 65 | # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 66 | # config.disable_monkey_patching! 67 | # 68 | # # This setting enables warnings. It's recommended, but in some cases may 69 | # # be too noisy due to issues in dependencies. 70 | # config.warnings = true 71 | # 72 | # # Many RSpec users commonly either run the entire suite or an individual 73 | # # file, and it's useful to allow more verbose output when running an 74 | # # individual spec file. 75 | # if config.files_to_run.one? 76 | # # Use the documentation formatter for detailed output, 77 | # # unless a formatter has already been configured 78 | # # (e.g. via a command-line flag). 79 | # config.default_formatter = "doc" 80 | # end 81 | # 82 | # # Print the 10 slowest examples and example groups at the 83 | # # end of the spec run, to help surface which specs are running 84 | # # particularly slow. 85 | # config.profile_examples = 10 86 | # 87 | # # Run specs in random order to surface order dependencies. If you find an 88 | # # order dependency and want to debug it, you can fix the order by providing 89 | # # the seed, which is printed after each run. 90 | # # --seed 1234 91 | # config.order = :random 92 | # 93 | # # Seed global randomization in this process using the `--seed` CLI option. 94 | # # Setting this allows you to use `--seed` to deterministically reproduce 95 | # # test failures related to randomization by passing the same `--seed` value 96 | # # as the one that triggered the failure. 97 | # Kernel.srand config.seed 98 | end 99 | -------------------------------------------------------------------------------- /spec/test_spec.rb: -------------------------------------------------------------------------------- 1 | # rubocop:disable Metrics/LineLength 2 | require_relative '../enumerable.rb' 3 | 4 | describe Enumerable do 5 | let(:array) { [1, 2, 3, 4, 5, 7, 8, 22, 55, 34, 86] } 6 | let(:all_odd_array) { [1, 3, 5, 7, 9, 11] } 7 | let(:negative_array) { [-8, -9, -6] } 8 | let(:false_array) { [1, false, 'hi', []] } 9 | describe '#my_each' do 10 | it 'return an array' do 11 | expect(array.my_all?(1)).to eq(array.all?(1)) 12 | end 13 | end 14 | 15 | describe '#my_each_with_index' do 16 | it 'return the array values' do 17 | expect(array.my_each_with_index { |elem, idx| }).to eq(array.each_with_index { |elem, idx| }) 18 | end 19 | end 20 | 21 | describe '#my_select' do 22 | it 'my_select should return odd array' do 23 | expect(array.my_select(&:odd?)).to eq(array.select(&:odd?)) 24 | end 25 | end 26 | 27 | describe '#my_select' do 28 | it 'my_select should return even array' do 29 | expect(array.my_select(&:even?)).to eq(array.select(&:even?)) 30 | end 31 | end 32 | 33 | describe '#my_all' do 34 | it 'my_all should return true on odd array' do 35 | expect(all_odd_array.my_all?(&:odd?)).to eq(all_odd_array.all?(&:odd?)) 36 | end 37 | end 38 | 39 | describe '#my_all' do 40 | it 'my_all should return true if all values are neagtive' do 41 | expect(negative_array.my_all? { |n| n < 0 }).to eq(negative_array.all? { |n| n < 0 }) 42 | end 43 | end 44 | 45 | describe '#my_all' do 46 | it 'my_all should return true if all values are neagtive' do 47 | expect(false_array.my_all?).to eq(false_array.all?) 48 | end 49 | end 50 | describe '#my_any' do 51 | it 'my_any should return true if values are even' do 52 | expect(array.my_any?(&:even?)).to eq(array.any?(&:even?)) 53 | end 54 | end 55 | 56 | describe '#my_any' do 57 | it 'my_any should return true if all charecters are in string' do 58 | expect(%w[q r s i].my_any? { |char| 'aeiou'.include?(char) }).to eq(%w[q r s i].any? { |char| 'aeiou'.include?(char) }) 59 | end 60 | end 61 | 62 | describe '#my_any' do 63 | it 'my_any should return false if numbers in array not even' do 64 | expect(all_odd_array.my_any?(&:even?)).to eq(all_odd_array.any?(&:even?)) 65 | end 66 | end 67 | 68 | describe '#my_any' do 69 | it 'my_any should return false if char are not in string' do 70 | expect(%w[q r s t].my_any? { |char| 'aeiou'.include?(char) }).to eq(%w[q r s t].any? { |char| 'aeiou'.include?(char) }) 71 | end 72 | end 73 | 74 | describe '#my_any' do 75 | it 'my_any should return true' do 76 | expect([1, nil, false].my_any?(1)).to eq([1, nil, false].any?(1)) 77 | end 78 | end 79 | 80 | describe '#my_any' do 81 | it 'my_any should return true if there a integer in array' do 82 | expect([1, nil, false].my_any?(Integer)).to eq([1, nil, false].any?(Integer)) 83 | end 84 | end 85 | describe '#my_any' do 86 | it 'my_any should return false if a charecter is not found in any elememt' do 87 | expect(%w[dog door rod blade].my_any?(/z/)).to eq(%w[dog door rod blade].any?(/z/)) 88 | end 89 | end 90 | 91 | describe '#my_any' do 92 | it 'my_any should return true if 1 is found in array' do 93 | expect(array.my_any?(1)).to eq(array.any?(1)) 94 | end 95 | end 96 | 97 | describe '#my_any' do 98 | it 'my_any should return true if elements are Numeric in array' do 99 | expect(array.my_any?(Numeric)).to eq(array.any?(Numeric)) 100 | end 101 | end 102 | 103 | describe '#my_any' do 104 | it 'my_any should return true if elements are Integer in array' do 105 | expect(array.my_any?(Integer)).to eq(array.any?(Integer)) 106 | end 107 | end 108 | 109 | describe '#my_any' do 110 | it 'my_any should return false if element is found' do 111 | expect(%w[jes,umair,jesagain,hello].my_any?('jes')).to eq(%w[jes,umair,jesagain,hello].any?('jes')) 112 | end 113 | end 114 | 115 | describe '#my_none' do 116 | it 'my_none should return true if the elements are even ' do 117 | expect(all_odd_array.my_none?(&:even?)).to eq(all_odd_array.none?(&:even?)) 118 | end 119 | end 120 | 121 | describe '#my_none' do 122 | it 'my_none should return true if a charecter is not found in a element of array ' do 123 | expect(%w[sushi pizza burrito].my_none? { |word| word[0] == 'a' }).to eq(%w[sushi pizza burrito].none? { |word| word[0] == 'a' }) 124 | end 125 | end 126 | 127 | describe '#my_none' do 128 | it 'my_none should return true if the elements are not even ' do 129 | expect(array.my_none?(&:even?)).to eq(array.none?(&:even?)) 130 | end 131 | end 132 | 133 | describe '#my_none' do 134 | it 'my_none should return false if every element is not starting with a ' do 135 | expect(%w[asparagus sushi pizza apple burrito].my_none? { |word| word[0] == 'a' }).to eq(%w[asparagus sushi pizza apple burrito].none? { |word| word[0] == 'a' }) 136 | end 137 | end 138 | 139 | describe '#my_none' do 140 | it 'my_none should return false if every element is not starting with a ' do 141 | expect([nil, false, nil, false].my_none?).to eq([nil, false, nil, false].none?) 142 | end 143 | end 144 | 145 | describe '#my_none' do 146 | it 'my_none should return false beacuse array is not none ' do 147 | expect(array.my_none?).to eq(array.none?) 148 | end 149 | end 150 | 151 | describe '#my_none' do 152 | it 'my_none should return true beacuse array are string ' do 153 | expect(array.my_none?(String)).to eq(array.none?(String)) 154 | end 155 | end 156 | 157 | describe '#my_none' do 158 | it 'my_none should return false beacuse there is 2 in array' do 159 | expect(array.my_none?(2)).to eq(array.none?(2)) 160 | end 161 | end 162 | 163 | describe '#my_none' do 164 | it 'my_none should return true because there is no 4 four in array' do 165 | expect([1, 2, 3].my_none?(4)).to eq([1, 2, 3].my_none?(4)) 166 | end 167 | end 168 | 169 | describe '#my_count' do 170 | it 'my_count should return count of even numbers' do 171 | expect(array.my_count(&:even?)).to eq(array.count(&:even?)) 172 | end 173 | end 174 | 175 | describe '#my_count' do 176 | it 'my_count should return count of uper case elements' do 177 | expect(%w[DANIEL JIA KRITI dave].my_count { |s| s == s.upcase }).to eq(%w[DANIEL JIA KRITI dave].count { |s| s == s.upcase }) 178 | end 179 | end 180 | 181 | describe '#my_count' do 182 | it 'my_count should return count of uper case elements' do 183 | expect(%w[DANIEL JIA KRITI dave].my_count { |s| s == s.upcase }).to eq(%w[DANIEL JIA KRITI dave].count { |s| s == s.upcase }) 184 | end 185 | end 186 | 187 | describe '#my_count' do 188 | it 'my_count should return count 1 in array' do 189 | expect(array.my_count(1)).to eq(array.count(1)) 190 | end 191 | end 192 | 193 | describe '#my_count' do 194 | it 'my_count should return elements from 1 to 5' do 195 | expect((1...5).my_count).to eq((1...5).count) 196 | end 197 | end 198 | 199 | describe '#my_map' do 200 | it 'my_map should return an array of multiply for 2' do 201 | expect(array.my_map { |n| 2 * n }).to eq(array.map { |n| 2 * n }) 202 | end 203 | end 204 | 205 | describe '#my_map' do 206 | it 'my_map should return an array with concat of ?' do 207 | expect(%w[Hey Jude].my_map { |word| word + '?' }).to eq(%w[Hey Jude].map { |word| word + '?' }) 208 | end 209 | end 210 | 211 | describe '#my_map' do 212 | it 'my_map should return with not operation' do 213 | expect([false, true].my_map(&:!)).to eq([false, true].map(&:!)) 214 | end 215 | end 216 | 217 | describe '#my_inject' do 218 | it 'my_inject will return the the array + 10' do 219 | expect(array.my_inject(10) { |accum, elem| accum + elem }).to eq(array.inject(10) { |accum, elem| accum + elem }) 220 | end 221 | it 'my_inject will return the sum of array' do 222 | expect(array.my_inject { |accum, elem| accum + elem }).to eq(array.inject { |accum, elem| accum + elem }) 223 | end 224 | it 'my_inject will return the multiplication with 2 of array with * symbol' do 225 | expect(array.my_inject(2, :*)).to eq(array.inject(2, :*)) 226 | end 227 | it '#my_inject raises a "LocalJumpError" when no block or argument is given Failure/Error' do 228 | expect { array.my_inject }.to raise_error(LocalJumpError) 229 | end 230 | end 231 | end 232 | 233 | # rubocop:enable Metrics/LineLength 234 | --------------------------------------------------------------------------------