Module: Enumerable

Defined in:
lib/qualitysmith_extensions/symbol/match.rb,
lib/qualitysmith_extensions/enumerable/enum.rb,
lib/qualitysmith_extensions/enumerable/select_while.rb

Instance Method Summary collapse

Instance Method Details

#enum(iterator = :each) ⇒ Object

The core Enumerable module provides the following enumerator methods;

  • enum_cons()

  • enum_slice()

  • enum_with_index()

but for some reason they didn’t provide a generic enum() method for the cases they didn’t think of!

enum lets you turn any iterator into a general-purpose Enumerator, which, according to the RDocs, is “A class which provides a method ‘each’ to be used as an Enumerable object.”

This lets you turn any ‘each'-type iterator (each_byte, each_line, …) into a ’map‘-type iterator (one that returns a collection), or into an array, etc.

So if an object responds to :each_line but not to :map_lines or :lines, you could just do:

object.enum(:each_line).map { block }
object.enum(:each_line).min
object.enum(:each_line).grep /pattern/
lines = object.enum(:each_line).to_a

If no iterator is specified, :each is assumed:

object.enum.map { block }

More examples:

Dir.new('.').enum.to_a
#=> ['file1', 'file2']

"abc".enum(:each_byte).map{|byte| byte.chr.upcase}
#=> ["A", "B", "C"]


39
40
41
# File 'lib/qualitysmith_extensions/enumerable/enum.rb', line 39

def enum(iterator = :each)
  Enumerable::Enumerator.new(self, iterator)
end

#grep_with_regexp_support_for_symbols(pattern, &block) ⇒ Object



88
89
90
91
92
# File 'lib/qualitysmith_extensions/symbol/match.rb', line 88

def grep_with_regexp_support_for_symbols(pattern, &block)
  map { |element|
    element.is_a?(Symbol) ? element.to_s : element
  }.grep_without_regexp_support_for_symbols(pattern, &block)
end

#select_until(inclusive = false, &block) ⇒ Object

Returns an array containing all consecutive elements of enum for which block is not false, starting at the first element. So it is very much like select, only it stops searching as soon as block ceases to be true. (That means it will stop searching immediately if the first element doesn’t match.)

This might be preferable to select, for example, if:

  • you have a very large collection of elements

  • the desired elements are expected to all be consecutively occuring and are all at the beginning of the collection

  • it would be costly to continue iterating all the way to the very end

This is probably only useful for collections that have some kind of predictable ordering (such as Arrays).

AKA: select_top_elements_that_match



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/qualitysmith_extensions/enumerable/select_while.rb', line 24

def select_until(inclusive = false, &block)
  selected = []
  inclusive ? (
    each do |item|
      selected << item
      break if block.call(item)
    end
  ) : (
    each do |item|
      break if block.call(item)
      selected << item
    end
  )
  selected
end

#select_while(&block) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/qualitysmith_extensions/enumerable/select_while.rb', line 40

def select_while(&block)
  selected = []
  each do |item|
    break if !block.call(item)
    selected << item
  end
  selected
end