-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from EdCrux/add-enumerable
Recreating Ruby's Enumerable
- Loading branch information
Showing
6 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
AllCops: | ||
Exclude: | ||
- "README.md" | ||
- "Guardfile" | ||
- "Rakefile" | ||
|
||
DisplayCopNames: true | ||
|
||
Layout/LineLength: | ||
Max: 125 | ||
Metrics/MethodLength: | ||
Max: 20 | ||
Metrics/AbcSize: | ||
Max: 50 | ||
Metrics/ClassLength: | ||
Max: 150 | ||
Metrics/BlockLength: | ||
ExcludedMethods: ['describe'] | ||
Max: 30 | ||
Metrics/ModuleLength: | ||
Max: 500 | ||
Metrics/CyclomaticComplexity: | ||
Max: 20 | ||
Metrics/PerceivedComplexity: | ||
Max: 20 | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
Style/ClassAndModuleChildren: | ||
Enabled: false | ||
Style/EachForSimpleLoop: | ||
Enabled: false | ||
Style/AndOr: | ||
Enabled: false | ||
Style/DefWithParentheses: | ||
Enabled: false | ||
Style/FrozenStringLiteralComment: | ||
EnforcedStyle: never | ||
Style/CaseEquality: | ||
Enabled: false | ||
|
||
Layout/HashAlignment: | ||
EnforcedColonStyle: key | ||
Layout/ExtraSpacing: | ||
AllowForAlignment: false | ||
Layout/MultilineMethodCallIndentation: | ||
Enabled: true | ||
EnforcedStyle: indented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# add the linters you want stickler to use for this project | ||
linters: | ||
rubocop: | ||
display_cop_names: true | ||
# indicate where is the config file for stylelint | ||
config: "./rubocop.yml" | ||
|
||
files: | ||
ignore: | ||
- "Guardfile" | ||
- "Rakefile" | ||
- "node_modules/**/*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source 'https://rubygems.org' | ||
git_source(:github) { "https://github.com/#{repo_name}" } | ||
gem 'rubocop' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ast (2.4.0) | ||
byebug (11.1.1) | ||
jaro_winkler (1.5.4) | ||
parallel (1.19.1) | ||
parser (2.7.0.2) | ||
ast (~> 2.4.0) | ||
rainbow (3.0.0) | ||
rubocop (0.79.0) | ||
jaro_winkler (~> 1.5.1) | ||
parallel (~> 1.10) | ||
parser (>= 2.7.0.1) | ||
rainbow (>= 2.2.2, < 4.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 1.4.0, < 1.7) | ||
ruby-progressbar (1.10.1) | ||
unicode-display_width (1.6.1) | ||
|
||
PLATFORMS | ||
ruby | ||
x64-mingw32 | ||
|
||
DEPENDENCIES | ||
byebug | ||
rubocop | ||
|
||
BUNDLED WITH | ||
2.1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,70 @@ | ||
# Enumerables-Ruby-methods | ||
In this repository I want to show the main methods found in the module Enumerables en Ruby | ||
|
||
In this repository I rebuild the main methods found in the module Enumerable. | ||
|
||
<img src="https://gohighbrow.com/wp-content/uploads/2018/01/7_An-introduction-to-the-Java-programming.jpg" width="100%"> | ||
|
||
--- | ||
|
||
> Enumerables project as part of microverse curriculum | ||
--- | ||
|
||
## Built With | ||
|
||
- Ruby | ||
|
||
## Live Demo | ||
|
||
[Live Demo Link](https://repl.it/@EdCrux/Enumerables-Ruby-methods) | ||
|
||
## Getting Started | ||
|
||
Install ruby 2.6.5 | ||
Clone the repo | ||
Bundle init | ||
|
||
### Prerequisites | ||
|
||
- Ruby version ~> 2.6 | ||
- IDE (if you want to test) | ||
|
||
### Dependencies | ||
|
||
- Rubocop | ||
|
||
### Install | ||
|
||
`bundle install` | ||
|
||
### Run tests | ||
|
||
Rubocop lint | ||
|
||
`rubocop enumerables.rb` | ||
|
||
### Deployment | ||
|
||
`ruby enumerables.rb` | ||
|
||
## Authors | ||
|
||
👤 **Eduardo Cruz** | ||
|
||
- Github: [EdCrux](https://github.com/EdCrux) | ||
- Twitter: [@Eduardo79973490](https://twitter.com/twitterhandle) | ||
- Linkedin: [linkedin](www.linkedin.com/in/edcrux) | ||
|
||
## 🤝 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! | ||
|
||
## 📝 License | ||
|
||
This project is [MIT](lic.url) licensed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
module Enumerable | ||
def my_each | ||
return to_enum(:my_each) unless block_given? | ||
|
||
i = 0 | ||
if is_a? Array | ||
while i <= length - 1 | ||
yield (self[i]) | ||
i += 1 | ||
end | ||
elsif is_a? Hash | ||
arr = to_a | ||
while i <= length - 1 | ||
yield [arr[i][0], arr[i][1]] | ||
i += 1 | ||
end | ||
end | ||
self | ||
end | ||
|
||
def my_each_with_index | ||
return to_enum(:my_each_with_index) unless block_given? | ||
|
||
i = 0 | ||
if is_a? Array | ||
while i <= length - 1 | ||
yield [self[i], i] | ||
i += 1 | ||
end | ||
elsif is_a? Hash | ||
arr = to_a | ||
while i <= length - 1 | ||
yield [arr[i], i] | ||
i += 1 | ||
end | ||
end | ||
end | ||
|
||
def my_select | ||
return to_enum(:my_select) unless block_given? | ||
|
||
if is_a? Hash | ||
hashy = {} | ||
my_each { |item| hashy[item.to_a[0]] = item.to_a[1] if yield i.to_a[0], i.to_a[1] } | ||
else | ||
arr = [] | ||
to_a.my_each { |item| arr << item if yield(item) } | ||
arr | ||
end | ||
end | ||
|
||
def my_all?(data = nil) | ||
return my_none?(data) if block_given? && !data.nil? | ||
|
||
arr = to_a | ||
return true if arr.empty? | ||
|
||
if block_given? | ||
to_a.my_each { |item| return false unless yield(item) } | ||
elsif data.nil? && !block_given? | ||
to_a.my_each { |item| return false unless item } | ||
elsif data.is_a? Class | ||
to_a.my_each { |item| return false unless item.is_a? data } | ||
elsif data.is_a? Regexp | ||
to_a.my_each { |item| return false unless item.to_s.match(data) } | ||
elsif data | ||
to_a.my_each { |item| return false unless item == data } | ||
end | ||
true | ||
end | ||
|
||
def my_any?(data = nil) | ||
return my_any?(data) if block_given? && !data.nil? | ||
|
||
if block_given? | ||
to_a.my_each { |item| return true if yield item } | ||
false | ||
elsif data.is_a? Regexp | ||
to_a.my_each { |item| return true if item.to_s.match(data) } | ||
elsif data.is_a? Class | ||
to_a.my_each { |item| return true if item.is_a? data } | ||
elsif data | ||
to_a.my_each { |item| return true if item == data } | ||
elsif data.nil? | ||
to_a.my_each { |item| return true if item } | ||
end | ||
false | ||
end | ||
|
||
def my_none?(data = nil) | ||
return my_none?(data) if block_given? && !data.nil? | ||
|
||
if block_given? | ||
to_a.my_each { |item| return false if yield item } | ||
elsif data.is_a? Regexp | ||
to_a.my_each { |item| return false if item.to_s.match(data) } | ||
elsif data.is_a? Class | ||
to_a.my_each { |item| return false if item.is_a? data } | ||
elsif data.nil? | ||
to_a.my_each { |item| return false if item } | ||
elsif data | ||
to_a.my_each { |item| return false if item == data } | ||
end | ||
true | ||
end | ||
|
||
def my_count(*data) | ||
arr = to_a | ||
return 0 if arr.empty? | ||
|
||
counter = 0 | ||
if data[0] | ||
arr.my_each { |item| counter += 1 if item == data[0] } | ||
elsif block_given? | ||
arr.my_each { |item| counter += 1 if yield(item) } | ||
else | ||
arr.my_each { |item| counter += 1 if item } | ||
end | ||
counter | ||
end | ||
|
||
def my_map | ||
return to_enum(:my_map) unless block_given? | ||
|
||
map_array = [] | ||
to_a.my_each { |item| map_array << yield(item) } | ||
map_array | ||
end | ||
|
||
def my_inject(*initial) | ||
arr = to_a.dup | ||
return raise ArgumentError, 'Given arguments 0, expected 1' if initial.empty? && !block_given? | ||
|
||
memo = initial.length == 2 && arr.respond_to?(initial[1]) || initial.length == 1 && block_given? ? initial[0] : arr.shift | ||
sym = if initial.length == 2 | ||
initial[1] | ||
elsif !block_given? && initial.length == 1 && arr.respond_to?(initial[0]) | ||
initial[0] | ||
else | ||
false | ||
end | ||
arr.my_each { |item| memo = sym ? memo.send(sym, item) : yield(memo, item) } | ||
memo | ||
end | ||
|
||
def multiply_els(arrays) | ||
arrays.my_inject(:*) | ||
end | ||
end |