Sha256: f353f126102973d1647c12cf3ef3ff148a6fdf8b36492607da66239af96f2397

Contents?: true

Size: 691 Bytes

Versions: 26

Compression:

Stored size: 691 Bytes

Contents

module JSONAPIonify
  class CharacterRange
    include Enumerable

    class << self
      def [](*args)
        new(*args)
      end
    end

    def initialize(start_char, end_char)
      if [start_char, end_char].any? { |c| c.length > 1 }
        raise ArgumentError, 'must be single characters'
      end
      @start_char = start_char
      @end_char   = end_char
    end

    def each
      range.each do |ord|
        begin
          char = ord.chr(Encoding::UTF_8)
          yield char
        rescue RangeError
          next
        end
      end
    end

    def to_a
      each.to_a
    end

    private

    def range
      (@start_char.ord)..(@end_char.ord)
    end

  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/character_range.rb
jsonapionify-0.12.9 lib/jsonapionify/character_range.rb
jsonapionify-0.12.8 lib/jsonapionify/character_range.rb
jsonapionify-0.12.7 lib/jsonapionify/character_range.rb
jsonapionify-0.12.5 lib/jsonapionify/character_range.rb
jsonapionify-0.12.4 lib/jsonapionify/character_range.rb
jsonapionify-0.12.3 lib/jsonapionify/character_range.rb
jsonapionify-0.12.2 lib/jsonapionify/character_range.rb
jsonapionify-0.12.1 lib/jsonapionify/character_range.rb
jsonapionify-0.12.0 lib/jsonapionify/character_range.rb
jsonapionify-0.11.11 lib/jsonapionify/character_range.rb
jsonapionify-0.11.10 lib/jsonapionify/character_range.rb
jsonapionify-0.11.9 lib/jsonapionify/character_range.rb
jsonapionify-0.11.8 lib/jsonapionify/character_range.rb
jsonapionify-0.11.7 lib/jsonapionify/character_range.rb
jsonapionify-0.11.6 lib/jsonapionify/character_range.rb
jsonapionify-0.11.5 lib/jsonapionify/character_range.rb
jsonapionify-0.11.0 lib/jsonapionify/character_range.rb
jsonapionify-0.10.2 lib/jsonapionify/character_range.rb
jsonapionify-0.10.1 lib/jsonapionify/character_range.rb