-
Notifications
You must be signed in to change notification settings - Fork 0
Development #1
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
base: master
Are you sure you want to change the base?
Development #1
Changes from all commits
f89c8f2
8fd9bee
d438c17
0e856fc
546cf4c
f4f21a4
4bd7842
17ef468
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| # /usr/bin/ruby | ||
| # frozen_string_literal: true | ||
|
|
||
|
|
||
|
|
||
| module Enumerable | ||
|
|
||
| def my_each #This is my_each method | ||
| a = 0 | ||
| self.size.times do | ||
| yield(self[a]) | ||
| a += 1 | ||
|
|
||
| end | ||
|
|
||
| self | ||
| end | ||
|
|
||
| def my_each_with_index #my_each_with_index | ||
| a = 0 | ||
| self.size.times do | ||
| yield(self[a], a) | ||
| a += 1 | ||
| end | ||
|
|
||
| self | ||
|
|
||
| end | ||
|
Comment on lines
+19
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return an enumerator irb(main):014:0> array.each_with_index
=> #<Enumerator: [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]:each_with_index>
irb(main):015:0> array.my_each_with_index
Traceback (most recent call last):
5: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
4: from (irb):15
3: from Enumerable_Methods.rb:21:in `my_each_with_index'
2: from Enumerable_Methods.rb:21:in `times'
1: from Enumerable_Methods.rb:22:in `block in my_each_with_index'
LocalJumpError (no block given (yield))
irb(main):016:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):017:0> |
||
|
|
||
| def my_select #my_select method | ||
| myselect = [] | ||
| self.my_each do |b| | ||
| if yield (b) | ||
| myselect.push (b) | ||
|
|
||
| end | ||
|
|
||
| end | ||
|
|
||
| myselect | ||
|
|
||
| end | ||
|
Comment on lines
+30
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return an enumerator irb(main):019:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):020:0> array.select
=> #<Enumerator: [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]:select>
irb(main):021:0> array.my_select
Traceback (most recent call last):
7: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
6: from (irb):21
5: from Enumerable_Methods.rb:32:in `my_select'
4: from Enumerable_Methods.rb:10:in `my_each'
3: from Enumerable_Methods.rb:10:in `times'
2: from Enumerable_Methods.rb:11:in `block in my_each'
1: from Enumerable_Methods.rb:33:in `block in my_select'
LocalJumpError (no block given (yield))
irb(main):022:0> |
||
|
|
||
| def my_all? #my_all method | ||
| myall = [] | ||
| output = true | ||
| self.my_each do |b| | ||
| if yield (b) | ||
| myall.push (b) | ||
| end | ||
| end | ||
| if myall.length == self.length | ||
| output = true | ||
| else | ||
| output = false | ||
| end | ||
| output | ||
| end | ||
|
Comment on lines
+44
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):026:0> true_array = [1, true, 'hi', []]
=> [1, true, "hi", []]
irb(main):027:0> true_array.all?
=> true
irb(main):028:0> true_array.my_all?
Traceback (most recent call last):
7: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
6: from (irb):28
5: from Enumerable_Methods.rb:47:in `my_all?'
4: from Enumerable_Methods.rb:10:in `my_each'
3: from Enumerable_Methods.rb:10:in `times'
2: from Enumerable_Methods.rb:11:in `block in my_each'
1: from Enumerable_Methods.rb:48:in `block in my_all?'
LocalJumpError (no block given (yield))
irb(main):029:0> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):031:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):032:0> array.all?(Integer)
=> true
irb(main):033:0> array.my_all?(Integer)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):33
1: from Enumerable_Methods.rb:44:in `my_all?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):034:0> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):042:0> words
=> ["dog", "door", "rod", "blade"]
irb(main):043:0> words.all?(/d/)
=> true
irb(main):044:0> words.my_all?(/d/)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):44
1: from Enumerable_Methods.rb:44:in `my_all?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):045:0> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):047:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):048:0> array.all?(3)
=> false
irb(main):049:0> array.my_all?(3)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):49
1: from Enumerable_Methods.rb:44:in `my_all?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):050:0> |
||
|
|
||
| def my_any? #my_any? method | ||
| output = false | ||
| self.my_each do |b| | ||
| if yield (b) | ||
| output = true | ||
| break | ||
|
|
||
| end | ||
|
|
||
| end | ||
| output | ||
| end | ||
|
Comment on lines
+60
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):052:0> true_array = [nil, false, true, []]
=> [nil, false, true, []]
irb(main):053:0> true_array.any?
=> true
irb(main):054:0> true_array.my_any?
Traceback (most recent call last):
7: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
6: from (irb):54
5: from Enumerable_Methods.rb:62:in `my_any?'
4: from Enumerable_Methods.rb:10:in `my_each'
3: from Enumerable_Methods.rb:10:in `times'
2: from Enumerable_Methods.rb:11:in `block in my_each'
1: from Enumerable_Methods.rb:63:in `block in my_any?'
LocalJumpError (no block given (yield))
irb(main):055:0>There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):057:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):058:0> array.any?(Integer)
=> true
irb(main):059:0> array.my_any?(Integer)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):59
1: from Enumerable_Methods.rb:60:in `my_any?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):060:0>There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):062:0> words
=> ["dog", "door", "rod", "blade"]
irb(main):063:0> words.any?(/z/)
=> false
irb(main):064:0> words.my_any?(/z/)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):64
1: from Enumerable_Methods.rb:60:in `my_any?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):065:0>There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):067:0> words[0] = 'cat'
=> "cat"
irb(main):068:0> words
=> ["cat", "door", "rod", "blade"]
irb(main):069:0> words.any?('cat')
=> true
irb(main):071:0> words.my_any?('cat')
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):71
1: from Enumerable_Methods.rb:60:in `my_any?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):072:0> |
||
|
|
||
| def my_none? #my_none methods | ||
| output = true | ||
| self.my_each do |b| | ||
| if yield (b) | ||
| output = false | ||
| break | ||
| end | ||
| end | ||
| output | ||
| end | ||
|
Comment on lines
+73
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):074:0> false_array = [nil, false, nil, false]
=> [nil, false, nil, false]
irb(main):075:0> false_array.none?
=> true
irb(main):076:0> false_array.my_none?
Traceback (most recent call last):
7: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
6: from (irb):76
5: from Enumerable_Methods.rb:75:in `my_none?'
4: from Enumerable_Methods.rb:10:in `my_each'
3: from Enumerable_Methods.rb:10:in `times'
2: from Enumerable_Methods.rb:11:in `block in my_each'
1: from Enumerable_Methods.rb:76:in `block in my_none?'
LocalJumpError (no block given (yield))
irb(main):077:0>There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):079:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):080:0> array.none?(String)
=> true
irb(main):081:0> array.my_none?(String)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):81
1: from Enumerable_Methods.rb:73:in `my_none?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):082:0> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):085:0> words = %w[dog door rod blade]
=> ["dog", "door", "rod", "blade"]
irb(main):086:0> words.none?(/z/)
=> true
irb(main):087:0> words.my_none?(/z/)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):87
1: from Enumerable_Methods.rb:73:in `my_none?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):088:0> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):091:0> words[0] = 5
=> 5
irb(main):092:0> words.none?(5)
=> false
irb(main):093:0> words.my_none?(5)
Traceback (most recent call last):
3: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):93
1: from Enumerable_Methods.rb:73:in `my_none?'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):094:0> words
=> [5, "door", "rod", "blade"]
irb(main):095:0> |
||
|
|
||
| def my_count parameter = nil #my_count method | ||
| mycount = 0 | ||
| self.my_each do |b| | ||
| if parameter != nil | ||
| if parameter == self[b] | ||
| mycount += 1 | ||
| end | ||
| else | ||
| if yield (b) | ||
| mycount += 1 | ||
| self.my_each do |element| | ||
| if parameter | ||
| if element == parameter | ||
| mycount += 1 | ||
| end | ||
| end | ||
| elsif block_given? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you investigate this line? It's causing an error. Please let's fix this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| mycount += if yield(element) | ||
| else | ||
| mycount = self.length | ||
| end | ||
| end | ||
| mycount | ||
| end | ||
|
Comment on lines
+84
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):097:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):098:0> array.count
=> 100
irb(main):099:0> array.my_count
Traceback (most recent call last):
7: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
6: from (irb):99
5: from Enumerable_Methods.rb:86:in `my_count'
4: from Enumerable_Methods.rb:10:in `my_each'
3: from Enumerable_Methods.rb:10:in `times'
2: from Enumerable_Methods.rb:11:in `block in my_each'
1: from Enumerable_Methods.rb:92:in `block in my_count'
LocalJumpError (no block given (yield))
irb(main):100:0> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should count the number of element not returning an array irb(main):102:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):103:0> array.count(3)
=> 14
irb(main):104:0> array.my_count(3)
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):105:0> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. irb(main):107:0> block = proc { |num| num < 6 }
=> #<Proc:0x000056229934e0b0@(irb):107>
irb(main):108:0> array.count(&block)
=> 65
irb(main):109:0> array.my_count(&block)
Traceback (most recent call last):
11: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
10: from (irb):109
9: from Enumerable_Methods.rb:86:in `my_count'
8: from Enumerable_Methods.rb:10:in `my_each'
7: from Enumerable_Methods.rb:10:in `times'
6: from Enumerable_Methods.rb:11:in `block in my_each'
5: from Enumerable_Methods.rb:94:in `block in my_count'
4: from Enumerable_Methods.rb:10:in `my_each'
3: from Enumerable_Methods.rb:10:in `times'
2: from Enumerable_Methods.rb:11:in `block in my_each'
1: from Enumerable_Methods.rb:100:in `block (2 levels) in my_count'
NoMethodError (undefined method `elif' for #<Array:0x00005622993417e8>)
irb(main):110:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):111:0> |
||
|
|
||
| def my_map #my_map method | ||
| mymap = [] | ||
| self.my_each do |b| | ||
| mymap.push yield(b) | ||
| end | ||
| mymap | ||
| end | ||
|
Comment on lines
+109
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return an enumerator when no block given irb(main):113:0> array
=> [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]
irb(main):114:0> array.map
=> #<Enumerator: [4, 3, 1, 6, 3, 2, 1, 8, 6, 0, 2, 2, 5, 2, 8, 4, 1, 1, 1, 4, 1, 6, 7, 2, 2, 8, 5, 5, 2, 0, 6, 7, 4, 3, 5, 5, 4, 7, 4, 7, 8, 0, 5, 4, 8, 7, 6, 5, 2, 7, 0, 3, 7, 4, 3, 8, 7, 2, 3, 5, 7, 3, 3, 4, 8, 0, 1, 3, 3, 4, 4, 7, 5, 6, 6, 6, 3, 8, 8, 8, 3, 7, 6, 5, 0, 3, 3, 1, 0, 0, 8, 4, 2, 7, 0, 8, 6, 0, 2, 8]:map>
irb(main):115:0> array.my_map
Traceback (most recent call last):
7: from /home/elvisfosso/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
6: from (irb):115
5: from Enumerable_Methods.rb:114:in `my_map'
4: from Enumerable_Methods.rb:10:in `my_each'
3: from Enumerable_Methods.rb:10:in `times'
2: from Enumerable_Methods.rb:11:in `block in my_each'
1: from Enumerable_Methods.rb:115:in `block in my_map'
LocalJumpError (no block given (yield))
irb(main):116:0> |
||
|
|
||
| def my_map_again(myproc) #my_mapa_gain method which takes a proc | ||
| self.my_each do |b| | ||
| mymapagain.push myproc.call(b) | ||
| end | ||
| mymapagain | ||
| end | ||
|
|
||
| def my_map_final myproc = nil #my_map_final method which an either take a proc or a block | ||
| mymapfinal = [] | ||
| self.my_each do |b| | ||
| if myproc == nil | ||
| mymapfinal.push yield(b) | ||
| else | ||
| mymapfinal.push myproc.call(b) | ||
| end | ||
| end | ||
| mymapfinal | ||
| end | ||
|
|
||
| def my_inject #my_inject method | ||
| output = nil | ||
| my_each = output ? yield (output, b) : self |0| | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This a ruby syntax error. 🙏 fix it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| end | ||
| output | ||
| end | ||
|
|
||
| def multiply_els #multiply_els method | ||
| my_inject {|mult, b | mult + b} | ||
|
|
||
| end | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| #Testing for my_each method | ||
| #(To test each of these methods, you can take it out of the comment and go ahead with the testing ) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, i think i have implemented some of these changes and have them in the feature_branch in this repo |
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12] | ||
| #arraysofnumbers.my_each do |numbers| | ||
| # numbers *= 5 | ||
| # print "#{numbers}" | ||
| #end | ||
|
|
||
| #Testing for my_each_with_index method | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 40] | ||
| #arraysofnumbers.my_each_with_index do |value, index| | ||
| #puts "arraysofnumbers index #{index} takes the value of #{value}" | ||
| #end | ||
|
|
||
| #Testing for my_select method | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 50] | ||
| #output = arraysofnumbers.my_select do |numbers| numbers % 2 != 0; end | ||
| #print output | ||
|
|
||
| #Testing for my_all method | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 40] | ||
| #output = arraysofnumbers.my_all? do |b| b % 2 == 0; end | ||
| #print output | ||
|
|
||
| #Testing for my_any method | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 25] | ||
| #output = arraysofnumbers.my_any? do |b| b < 3; end | ||
| #print output | ||
|
|
||
| #Testing for my_none method | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 10] | ||
| #output = arraysofnumbers.my_none? do |b| b % 2 == 1; end | ||
| #print output | ||
|
|
||
| #Testing for my_count method | ||
|
|
||
| #arraysofnumbers = [2, 5, 8, 10, 20, 15, 25, 15, 8, 4, 6] | ||
| #arraysofnumbers = [5, 8, 10, 15, 15, 8, 4, 6, 8, 2, 12, 7, 8, 10] | ||
| #output = arraysofnumbers.my_count | ||
| #output = arraysofnumbers.my_count (10) | ||
| #output = arraysofnumbers () do |numbers| numbers % 2 == 1; end | ||
| #output = arraysofnumbers (5) do |numbers|; end | ||
| #print output | ||
|
|
||
| #Testing for my_map method | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 20] | ||
| #output = arraysofumbers.my_map do |numbers| numbers * 2; end | ||
| #print output | ||
|
|
||
| #Testing for my_map_again method (that takes a proc) | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 30, 10, 10] | ||
| #myproc = Proc.new do |numbers| numbers * 2; end | ||
| #output = arraysofnumbers.my_map_again(myproc) | ||
| #print output | ||
|
|
||
| #Testing for my_map_final(3) (that takes either a proc or a block) | ||
|
|
||
| #arraysofnumbers = [2, 4, 6, 8, 10, 12, 20, 50, 19, 25, 35, 30] | ||
| #myproc = Proc.new do |numbers| numbers * 2; end | ||
| #output = arraysofnumbers.my_map_final do |numbers| numbers * 3; end | ||
| #print output | ||
|
|
||
| #Testing for my_inject method | ||
|
|
||
| #def multiply_els(arraysofnumbers) | ||
| #arraysofnumbers.my_inject(1) do |total, numbers|total * numbers; end | ||
| #end | ||
| #arraysofnumbers = [1, 4, 5, 3, 2] | ||
| #puts multiply_els(arraysofnumbers) | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add linters:
rubocop:
display_cop_names: true
files:
ignore:
- "bin/*"
- "db/*"
- "config/*"
- "Guardfile"
- "Rakefile"
If you are having an issue with setting up linter, read the You can run the linter locally: 💥 - run
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,13 @@ | ||
| # Enumerable-Methods | ||
| Microverse Project On Enumerable Methods | ||
| # Project Descriptioon: | ||
|
|
||
| Advanced Building Blocks - Enumerable Methods Using Ruby Language. | ||
|
|
||
| # Contributor(s): | ||
|
|
||
| Kingsley McSimon Ogbonna https://github.com/KingsleyMcSimon @KingsleyMcSimon | ||
|
|
||
| # Link to the project on Github: | ||
|
|
||
| https://github.com/KingsleyMcSimon/Enumerable-Methods | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a more descriptive readme. Details like assignment link (from Odin), list of custom methods built, their functions with examples and what was learned during the project can make the readme more descriptive. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should return an enumerator