Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreating Ruby's Enumerable #3

Merged
merged 23 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
28d140d
:sparkles: Adding Enumerables file
EdCrux Feb 12, 2020
f619418
:heavy_plus_sign: Adding a dependency
EdCrux Feb 12, 2020
da3a651
:sparkles: Introducing #my_each & #my_each_with_index
EdCrux Feb 12, 2020
5cba342
:sparkles: Adding my_select method
EdCrux Feb 13, 2020
cc83e29
Updating readmefile
EdCrux Feb 14, 2020
962787d
:bug: Fixing a bug with my_all? method
EdCrux Feb 14, 2020
48ea0aa
Merge branch 'add-enumerable' of https://github.com/EdCrux/Enumerable…
EdCrux Feb 14, 2020
a43c772
Merging
EdCrux Feb 14, 2020
a98338a
:sparkles: Introducing my_all? and my_any?
EdCrux Feb 17, 2020
b4d53b5
:sparkles: Introducing my_none?
EdCrux Feb 17, 2020
ca0e8b1
:sparkles: Introducing my_inject
EdCrux Feb 19, 2020
bb633c0
:bug: Fixing bugs my_all? and my_none? methods
EdCrux Feb 20, 2020
a22be66
:bug: Fixing a but related to my_none?
EdCrux Feb 20, 2020
95476c5
:rotating_light: Removing linter warnigns and debugging my_all? method
EdCrux Feb 20, 2020
c47ad74
Update Read.me
EdCrux Feb 20, 2020
bf7780b
:bug: Fixing bugs,
EdCrux Feb 21, 2020
6e0fbfb
:goal_net: Catching errors
EdCrux Feb 21, 2020
fa4f114
:bug: Fixing bugs over my_all?, my_none?, and my_any?
EdCrux Feb 24, 2020
87f3da4
:fire: Removing files
EdCrux Feb 24, 2020
213081c
:zap: Adding some logic to the method my_none? , my_any?
EdCrux Feb 24, 2020
cac652b
:rotating_light: Removing linter warnings
EdCrux Feb 24, 2020
7659a6f
:bug: Fixing a bug of my_any?
EdCrux Feb 24, 2020
278d488
:bug: Fixing a bug of my_inject method to return the same array.
EdCrux Feb 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions .rubocop.yml
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
12 changes: 12 additions & 0 deletions .stickler.yml
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/**/*"
3 changes: 3 additions & 0 deletions Gemfile
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'
30 changes: 30 additions & 0 deletions Gemfile.lock
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
70 changes: 69 additions & 1 deletion README.md
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.
149 changes: 149 additions & 0 deletions enumerables.rb
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