Sha256: c1a6cfaf3e004831c3ef8ee9d015c8b044a37e7befaade89704daf60eaab055a

Contents?: true

Size: 1.2 KB

Versions: 16

Compression:

Stored size: 1.2 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 intersection subtract union].each do |method|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{method}(arg)
          if arg.is_a?(CharacterSet)
            super
          elsif arg.respond_to?(:each)
            super(CharacterSet.new(arg.to_a))
          else
            raise ArgumentError, 'pass an enumerable'
          end
        end
      RUBY
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
character_set-1.4.1-java lib/character_set/set_method_adapters.rb
character_set-1.4.1 lib/character_set/set_method_adapters.rb
character_set-1.4.0-java lib/character_set/set_method_adapters.rb
character_set-1.4.0 lib/character_set/set_method_adapters.rb
character_set-1.3.0-java lib/character_set/set_method_adapters.rb
character_set-1.3.0 lib/character_set/set_method_adapters.rb
character_set-1.2.0-java lib/character_set/set_method_adapters.rb
character_set-1.2.0 lib/character_set/set_method_adapters.rb
character_set-1.1.2-java lib/character_set/set_method_adapters.rb
character_set-1.1.2 lib/character_set/set_method_adapters.rb
character_set-1.1.1-java lib/character_set/set_method_adapters.rb
character_set-1.1.1 lib/character_set/set_method_adapters.rb
character_set-1.1.0-java lib/character_set/set_method_adapters.rb
character_set-1.1.0 lib/character_set/set_method_adapters.rb
character_set-1.0.0-java lib/character_set/set_method_adapters.rb
character_set-1.0.0 lib/character_set/set_method_adapters.rb