Sha256: b7fc33d32bf8da0c63b22d99459300403c7bc1a070745c6fca8317b1235fb9b1
Contents?: true
Size: 852 Bytes
Versions: 34
Compression:
Stored size: 852 Bytes
Contents
# frozen_string_literal: true module Quby class RangeCategories def initialize(*range_categories) if range_categories.length.even? || range_categories.length < 3 fail "RangeCategories should be of the form (0, 'label 0-10', 10, 'label 10-20', 20)" end @range_hash = {} parse_ranges(range_categories) end def as_range_hash @range_hash.freeze end private def parse_ranges(range_categories) a = range_categories.dup while a.length > 1 from = a.shift.to_f label = a.shift to = a.first.to_f add_range(from, label, to, inclusive: a.length == 1) end end def add_range(from, label, to, inclusive:) if inclusive @range_hash[from..to] = label else @range_hash[from...to] = label end end end end
Version data entries
34 entries across 34 versions & 1 rubygems