File tree Expand file tree Collapse file tree 9 files changed +90
-0
lines changed
Challenges/Advanced Building Blocks Expand file tree Collapse file tree 9 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ def my_all
2+ a = true
3+ self . my_each do |e |
4+ if yield ( e ) == false
5+ a = false
6+ end
7+ end
8+ a
9+ end
Original file line number Diff line number Diff line change 1+ def my_any?
2+ a = false
3+ self . my_each do |e |
4+ if yield ( e ) == true
5+ a = true
6+ end
7+ end
8+ a
9+ end
Original file line number Diff line number Diff line change 1+ def my_count
2+ c = 0
3+ self . my_each do |e |
4+ if block_given?
5+ c += 1 if yield ( e ) == true
6+ else
7+ c += 1
8+ end
9+ end
10+ c
11+ end
Original file line number Diff line number Diff line change 1+ def my_each
2+ if self . class == Hash
3+ keys . each do |k |
4+ val = self [ k ]
5+ yield ( k , val )
6+ end
7+ else
8+ each do |i |
9+ yield i
10+ end
11+ end
12+ end
Original file line number Diff line number Diff line change 1+ def my_each_with_index
2+ i = 0
3+ for a in self
4+ yield ( a , i )
5+ i = i + 1
6+ end
7+ end
Original file line number Diff line number Diff line change 1+ def my_inject ( store = self [ ] )
2+ store ||= [ ]
3+ self . my_each do |e |
4+ store = yield ( store , e )
5+ end
6+ store
7+ end
Original file line number Diff line number Diff line change 1+ def my_map ( )
2+ mapping = [ ]
3+ if block_given?
4+ self . my_each do |e |
5+ mapping . push ( yield ( e ) )
6+ end
7+ else
8+ self . my_each do |e |
9+ mapping . push ( proc . call ( e ) )
10+ end
11+ end
12+ mapping
13+ end
Original file line number Diff line number Diff line change 1+ def my_none?
2+ a = true
3+ self . my_each do |e |
4+ if yield ( e ) == true
5+ a = false
6+ end
7+ end
8+ a
9+ end
Original file line number Diff line number Diff line change 1+ def my_select
2+ if self . class == Array
3+ ss = [ ]
4+ end
5+ if self . class == Hash
6+ ss = { }
7+ end
8+
9+ self . my_each do |e |
10+ ss . push ( e ) if yield ( e )
11+ end
12+ ss
13+ end
You can’t perform that action at this time.
0 commit comments