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

Version Path
quby-compiler-0.5.9 lib/quby/range_categories.rb
quby-compiler-0.5.8 lib/quby/range_categories.rb
quby-compiler-0.5.7 lib/quby/range_categories.rb
quby-compiler-0.5.6 lib/quby/range_categories.rb
quby-compiler-0.5.5 lib/quby/range_categories.rb
quby-compiler-0.5.4 lib/quby/range_categories.rb
quby-compiler-0.5.3 lib/quby/range_categories.rb
quby-compiler-0.5.2 lib/quby/range_categories.rb
quby-compiler-0.5.1 lib/quby/range_categories.rb
quby-compiler-0.5.0 lib/quby/range_categories.rb
quby-compiler-0.4.16 lib/quby/range_categories.rb
quby-compiler-0.4.15 lib/quby/range_categories.rb
quby-compiler-0.4.14 lib/quby/range_categories.rb
quby-compiler-0.4.13 lib/quby/range_categories.rb
quby-compiler-0.4.12 lib/quby/range_categories.rb
quby-compiler-0.4.11 lib/quby/range_categories.rb
quby-compiler-0.4.10 lib/quby/range_categories.rb
quby-compiler-0.4.9 lib/quby/range_categories.rb
quby-compiler-0.4.8 lib/quby/range_categories.rb
quby-compiler-0.4.7 lib/quby/range_categories.rb