Sha256: 1f048879eb50fa68c4b40eebc5bcb09c464d768874a66e7014e2980f7aa6f76e

Contents?: true

Size: 712 Bytes

Versions: 6

Compression:

Stored size: 712 Bytes

Contents

class String

  unless method_defined?(:each_char) # 1.9

    require 'strscan'

    # Iterates through each character. This is a little faster than
    # using #chars b/c it does not create the intermediate array.
    #
    #    a = ''
    #   "HELLO".each_character{ |c| a << #{c.downcase} }
    #    a  #=> 'hello'

    #def each_char  # :yield:
    #  size.times do |i|
    #    yield(self[i,1])
    #  end
    #end

    # Yields a single-character string for each character in the string.
    # When $KCODE = 'UTF8', multi-byte characters are yielded appropriately.
    def each_char
      scanner, char = StringScanner.new(self), /./mu
      loop { yield(scanner.scan(char) || break) }
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/string/each_char.rb
facets-2.8.3 lib/core/facets/string/each_char.rb
facets-2.8.2 lib/core/facets/string/each_char.rb
facets-2.8.1 lib/core/facets/string/each_char.rb
facets-2.8.0 lib/core/facets/string/each_char.rb
facets-2.7.0 lib/core/facets/string/each_char.rb