Module: Range::Bisect

Defined in:
lib/range/bisect/version.rb,
lib/range/bisect.rb

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bisect(integer_range) {|| ... } ⇒ Object

Extends Range::Bisect into a duplicate of integer_range, then calls #bisect.

Parameters:

  • integer_range (Range<Integer>)

Yield Parameters:

  • (Range<Integer>)

Yield Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/range/bisect.rb', line 11

def self.bisect(integer_range, &block)
  source = integer_range.dup
  source.extend(self) unless source.kind_of?(Range::Bisect)
  source.bisect(&block)
end

Instance Method Details

#bisect {|| ... } ⇒ Object

By bisection, find all of the elements in this range that cause the block to return true. It is especially important to note that the given block must operate with the whole range, not simply its beginning or end.

Yield Parameters:

  • (Range<Integer>)

Yield Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/range/bisect.rb', line 24

def bisect(&block)
  ensure_integer_range!

  return [] unless yield(self)

  if first(2).size == 1 # determine if single-element without #to_a
    return [first]
  else
    bisection.map do |range|
      Range::Bisect.bisect(range, &block)
    end.flatten.sort
  end
end