Ruby Hash / Array delete_if without block

According to the Ruby Hash / Array documentation, the method delete_ifreturns an enumerator if no block is specified. How is this useful? Can someone give an example to demonstrate this pattern?

+3
source share
2 answers

The enumerator will simply allow you to start the block later. For example, if you have a method that specifically handled deletion, if for several different objects, you can pass an enumerator to it.

In the example below, it will print 1, 3, 5

arr = [0,1,2,3,4,5]
enumerator = arr.delete_if
enumerator.each { |el| el.even? }
puts arr.join(', ')
+3
source

Enumerator , . , , with_index.

p %w[a b c d e f].delete_if.with_index{|_, i| i.even?}
# => ["b", "d", "f"]

Enumerator, , delete_if_with_index, .

+8

All Articles