Class: ClosestNeighbours::Grouper
- Inherits:
-
Object
- Object
- ClosestNeighbours::Grouper
- Defined in:
- lib/closest_neighbours/grouper.rb
Overview
Split an Enumerable into specified number of groups containing the closest elements in each group.
Defined Under Namespace
Classes: GrouperHelper
Instance Method Summary collapse
- #call(groups = 1, data = []) ⇒ Object
-
#initialize(orderer: DefaultOrder.new) ⇒ Grouper
constructor
A new instance of Grouper.
Constructor Details
#initialize(orderer: DefaultOrder.new) ⇒ Grouper
Returns a new instance of Grouper.
8 9 10 |
# File 'lib/closest_neighbours/grouper.rb', line 8 def initialize(orderer: DefaultOrder.new) @orderer = orderer end |
Instance Method Details
#call(groups = 1, data = []) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/closest_neighbours/grouper.rb', line 12 def call(groups = 1, data = []) raise NonIntegerGroupsError unless groups.is_a? Integer raise NonEnumberableArgumentError unless data.is_a? Enumerable raise InsufficientGroupsError if groups < 1 raise IncomparableElementError unless data.all? { |e| e <=> data.first } GrouperHelper.new(groups, orderer.call(data)).call end |