Sha256: ca99323a283c3634929c1afe71d620d757e651d29343a0562825013677ce611a

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

class CharacterSet
  module SetMethodAdapters
    # Allow some methods to work with String in addition to Integer args
    # (the internal representation is geared towards codepoint Integers).
    %w[add add? << delete delete? include? member? ===].each do |method|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{method}(arg)
          case arg
          when String
            super(arg.ord)
          when Integer
            if arg < 0 || arg > 0x10FFFF
              raise ArgumentError, 'pass an Integer between 0 and 0x10FFFF'
            end
            super(arg)
          else
            raise ArgumentError, 'pass a String or an Integer'
          end
        end
      RUBY
    end

    # Allow some methods to take an Enum just as well as another CharacterSet.
    # Tested by ruby-spec.
    %w[& + - ^ | difference disjoint? intersect? intersection
       subtract union].each do |method|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{method}(arg)
          if arg.is_a?(CharacterSet)
            super(arg)
          elsif arg.respond_to?(:each)
            super(self.class.new(arg.to_a))
          else
            raise ArgumentError, 'pass an enumerable'
          end
        end
      RUBY
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
character_set-1.7.0-java lib/character_set/set_method_adapters.rb
character_set-1.7.0 lib/character_set/set_method_adapters.rb
character_set-1.6.0-java lib/character_set/set_method_adapters.rb
character_set-1.6.0 lib/character_set/set_method_adapters.rb
character_set-1.5.0-java lib/character_set/set_method_adapters.rb
character_set-1.5.0 lib/character_set/set_method_adapters.rb