module Enumerable

Public Instance Methods

back(x) click to toggle source

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 #=> [

@param [Integer] x the number of elements to take @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 60
def back(x)
        reverse.take(x).reverse
end
back_while(&p) click to toggle source

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

@param [Block] p the predicate the to evaluate while considering to take the last element @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 71
def back_while(&p)
        reverse_each.take_while(&p).reverse
end
has?(search) click to toggle source

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

@param [Object] search target @return [Boolean]

# File lib/kirinnee_core/enumerable.rb, line 119
def has?(search)
        count(search) > 0
end
indexes(&p) click to toggle source

Gets the indexes that matches the predicate Does not mutate the original enumerable

6,5,4,3,2].indexes {|x| x < 4} # => [3,4

@param [Block] p the predicate to find indexes @return [Array<Integer>]

# 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) click to toggle source

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

@param [Integer] x the number of elements to omit @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 84
def omit(x)
        reverse_each.drop(x).reverse
end
omit_while(&p) click to toggle source

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

@param [Block] p the predicate to evaluate to consider to remove the last element @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 95
def omit_while(&p)
        reverse_each.drop_while(&p).reverse
end
remove(search) click to toggle source

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)

@param [Object] search the target to remove from the array @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 107
def remove(search)
        where {|x| x != search}
end
skip(x) click to toggle source

Skips the first x elements of the enumerable Does not mutate the original enumerable

1,2,3,4,5].skip 3 #=> [4,5

{:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}.skip 3 #=> {:d=>4, :e=>5}

@param [Integer] x the number of elements to skip @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 36
def skip(x)
        drop(x)
end
skip_while(&p) click to toggle source

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

@param [Block] p predicate to decide whether to skip @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 47
def skip_while(&p)
        drop_while(&p)
end
where(&f) click to toggle source

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

@param [Block] f predicate @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 11
def where(&f)
        select &f
end
where!(&f) click to toggle source

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

@param [Block] f predicate @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 23
def where!(&f)
        select! &f
end
without(w) click to toggle source

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]

@param [Array] w the elements to remove @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 130
def without(w)
        where {|x| !w.has?(x)}
end
without_index(index) click to toggle source

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]

{:a => 1, :b => 2, :c => 3}.without_index([0, 2]).to_h # => {:b=>2} @param [Array] index the elements to remove @return [Enumerable]

# File lib/kirinnee_core/enumerable.rb, line 141
def without_index(index)
        each_with_index.where {|_, i| !index.has?(i)}.map {|x, _| x}
end