Module: Enumerable
- Defined in:
- lib/kirinnee_core/enumerable.rb
Instance Method Summary collapse
-
#back(x) ⇒ Enumerable
Takes the last x element of the enumerable Does not mutate the original enumerable.
-
#back_while(&p) ⇒ Enumerable
Takes the last x element while predicate evaluates to true Does not mutate the original enumerable.
-
#has?(search) ⇒ Boolean
Whether the enumerable contains at least 1 element that matches the input.
-
#indexes(&p) ⇒ Array<Integer>
Gets the indexes that matches the predicate Does not mutate the original enumerable.
-
#omit(x) ⇒ Enumerable
Omits the last x element of the enumerable Does not mutate the original enumerable.
-
#omit_while(&p) ⇒ Enumerable
Omits the last element while the predicate evaluates to true Does not mutate the original enumerable.
-
#remove(search) ⇒ Enumerable
Removes all occurrences of the element within the enumerable Does not mutate the original enumerable.
-
#skip(x) ⇒ Enumerable
Skips the first x elements of the enumerable Does not mutate the original enumerable.
-
#skip_while(&p) ⇒ Enumerable
Skips the elements of the enumerable while the predicate evaluates to true Does not mutate the original enumerable.
-
#where(&f) ⇒ Enumerable
Filters base on predicate passed via block.
-
#where!(&f) ⇒ Enumerable
Filters base on predicate passed via block.
-
#without(w) ⇒ Enumerable
Remove all occurrences of each element in provided array from target array Does not mutate the original enumerable.
-
#without_index(index) ⇒ Enumerable
Remove all elements has index of the input array Does not mutate the original enumerable.
Instance Method Details
#back(x) ⇒ Enumerable
Takes the last x element of the enumerable Does not mutate the original enumerable
- 1,2,3,4,5,6].back 3 #=> [4,5,6
- 1,2,3,4,5,6].back 100 #=> [1,2,3,4,5,6
- 1,2,3,4,5,6].back 0 #=> [
60 61 62 |
# File 'lib/kirinnee_core/enumerable.rb', line 60 def back(x) reverse.take(x).reverse end |
#back_while(&p) ⇒ Enumerable
Takes the last x element while predicate evaluates to true Does not mutate the original enumerable
- 1,2,3,2,1,0].back_while {|x| x <2} # => [1,0
71 72 73 |
# File 'lib/kirinnee_core/enumerable.rb', line 71 def back_while(&p) reverse_each.take_while(&p).reverse end |
#has?(search) ⇒ Boolean
Whether the enumerable contains at least 1 element that matches the input
[1,2,3,4,5,6,1].has? 1 # true [1,2,3,4,5,6,1].has? 2 # true [1,2,3,4,5,6,1].has? 7 # false
119 120 121 |
# File 'lib/kirinnee_core/enumerable.rb', line 119 def has?(search) count(search) > 0 end |
#indexes(&p) ⇒ Array<Integer>
Gets the indexes that matches the predicate Does not mutate the original enumerable
- 6,5,4,3,2].indexes {|x| x < 4} # => [3,4
152 153 154 155 156 157 158 159 160 |
# File 'lib/kirinnee_core/enumerable.rb', line 152 def indexes(&p) ret = [] each_with_index do |x, i| if p.call(x, i) ret.append i end end ret end |
#omit(x) ⇒ Enumerable
Omits the last x element of the enumerable Does not mutate the original enumerable
- 1,2,3,4,5].omit 2 # => [1,2,3
- 1,2,3,4,5].omit 100 #=> [
- 1,2,3,4,5].omit 0 # => [1,2,3,4,5
84 85 86 |
# File 'lib/kirinnee_core/enumerable.rb', line 84 def omit(x) reverse_each.drop(x).reverse end |
#omit_while(&p) ⇒ Enumerable
Omits the last element while the predicate evaluates to true Does not mutate the original enumerable
- 1,2,3,2,1,0].omit_while { |x| x <3} #=> [1,2,3
95 96 97 |
# File 'lib/kirinnee_core/enumerable.rb', line 95 def omit_while(&p) reverse_each.drop_while(&p).reverse end |
#remove(search) ⇒ Enumerable
Removes all occurrences of the element within the enumerable Does not mutate the original enumerable
- 1,2,3,1,2,3].remove 3 #=> [1,2,1,2
-
%w(apple pear apple pear).remove “apple” #=> %w(pear pear)
107 108 109 |
# File 'lib/kirinnee_core/enumerable.rb', line 107 def remove(search) where {|x| x != search} end |
#skip(x) ⇒ Enumerable
Skips the first x elements of the enumerable Does not mutate the original enumerable
- 1,2,3,4,5].skip 3 #=> [4,5
-
:b=>2, :c=>3, :d=>4, :e=>5.skip 3 #=> :e=>5
36 37 38 |
# File 'lib/kirinnee_core/enumerable.rb', line 36 def skip(x) drop(x) end |
#skip_while(&p) ⇒ Enumerable
Skips the elements of the enumerable while the predicate evaluates to true Does not mutate the original enumerable
- 1,2,3,2,1,0].skip_while {|x| x < 3} # => [3,2,1,0
47 48 49 |
# File 'lib/kirinnee_core/enumerable.rb', line 47 def skip_while(&p) drop_while(&p) end |
#where(&f) ⇒ Enumerable
Filters base on predicate passed via block. Variant of “select, filter, find all” method in vanilla ruby Does not modify the original array or enumerable
- 1,2,3].where { |x| x%2!=0 } # => [1,3
11 12 13 |
# File 'lib/kirinnee_core/enumerable.rb', line 11 def where(&f) select &f end |
#where!(&f) ⇒ Enumerable
Filters base on predicate passed via block. Variant of “select, filter, find all” method in vanilla ruby Modifies the original array or enumerable
- 1,2,3].where! { |x| x%2!=0 } # => [1,3
23 24 25 |
# File 'lib/kirinnee_core/enumerable.rb', line 23 def where!(&f) select! &f end |
#without(w) ⇒ Enumerable
Remove all occurrences of each element in provided array from target array Does not mutate the original enumerable
- 1,2,3,4,1,2,3,4].without [2,4
-
#=> [1,3,1,3]
130 131 132 |
# File 'lib/kirinnee_core/enumerable.rb', line 130 def without(w) where {|x| !w.has?(x)} end |
#without_index(index) ⇒ Enumerable
Remove all elements has index of the input array Does not mutate the original enumerable
- 6,5,4,3,2].without_index [0,2
-
#=> [5,3,2]
=> 1, :b => 2, :c => 3.without_index([0, 2]).to_h # => :b=>2
141 142 143 |
# File 'lib/kirinnee_core/enumerable.rb', line 141 def without_index(index) each_with_index.where {|_, i| !index.has?(i)}.map {|x, _| x} end |