Module: Enumerable

Defined in:
lib/kirinnee_core/enumerable.rb

Instance Method Summary collapse

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

Parameters:

  • x (Integer)

    the number of elements to take

Returns:



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

Parameters:

  • p (Block)

    the predicate the to evaluate while considering to take the last element

Returns:



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

Parameters:

  • search (Object)

    target

Returns:

  • (Boolean)


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

Parameters:

  • p (Block)

    the predicate to find indexes

Returns:

  • (Array<Integer>)


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

Parameters:

  • x (Integer)

    the number of elements to omit

Returns:



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

Parameters:

  • p (Block)

    the predicate to evaluate to consider to remove the last element

Returns:



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)

Parameters:

  • search (Object)

    the target to remove from the array

Returns:



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

Parameters:

  • x (Integer)

    the number of elements to skip

Returns:



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

Parameters:

  • p (Block)

    predicate to decide whether to skip

Returns:



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

Parameters:

  • f (Block)

    predicate

Returns:



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

Parameters:

  • f (Block)

    predicate

Returns:



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]

Parameters:

  • w (Array)

    the elements to remove

Returns:



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

Parameters:

  • index (Array)

    the elements to remove

Returns:



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