The Enumerable module that gets mixed into the Array and Hash classes (among others) and provides lots of handy iterator methods. To prove that there's no magic to it, we are going to rebuild those methods.
-
Create
#my_each, a method that is identical to#eachbut (obviously) does not use#each. You'll need to remember the yield statement. Make sure it returns the same thing as #each as well. -
Create
#my_each_with_indexin the same way. -
Create
#my_selectin the same way, though you may use#my_eachin your definition (but not #each). -
Create
#my_all?(continue as above) -
Create
#my_any? -
Create
#my_none? -
Create
#my_count -
Create
#my_map -
Create
#my_inject -
Test your
#my_injectby creating a method called#multiply_elswhich multiplies all the elements of the array together by using#my_inject, e.g. multiply_els([2,4,5]) #=> 40 -
Modify your
#my_mapmethod to take a proc instead. -
Modify your
#my_mapmethod 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.
- Ruby
- VS Code
-
Boot up your terminal and punch in
gem install rspecto install RSpec. Once that’s done, you can verify your version of RSpec withrspec --version, which will output the current version of each of the packaged gems. Take a minute also to hitrspec --helpand look through the various options available. -
Finally, cd into a project directory that you wish to configure for use with RSpec and type
rspec --initto initialize RSpec within the project. This will generate two files,.rspecandspec/spec_helper.rb -
To check whether your test are passing or failing, go to your terminal and type
rspec.
👤 Ijay Abby
- Github: @IjayAbby
- Twitter: @Ijay_js
- LinkedIn: Abigael Nyangasi
Contributions, issues and feature requests are always welcome!
I love meeting other developers, especially ones that give me advice on how to improve my work.
Feel free to check the issues page.
Finally, if you've read this far, don't forget to give this repo a ⭐️. They're free . . . I think.
- This Project is available on The Odin Project.
- Thanks to Microverse and all our peers and colleagues there.
This project is MIT licensed.