Sha256: b98ddf26ed8e128f2de5ed44ad3c07db92c9bacc6e45427e009e7c160e2415bc

Contents?: true

Size: 735 Bytes

Versions: 7

Compression:

Stored size: 735 Bytes

Contents

class String

  unless method_defined?(:each_char) # 1.9

    require 'strscan'

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

    #--
    # Iterates through each character. This is a little faster than
    # using #chars b/c it does not create the intermediate array...
    #
    #   def each_char  # :yield:
    #     size.times{ |i| yield(self[i,1]) }
    #   end
    #++

  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/string/each_char.rb
facets-2.9.2 lib/core/facets/string/each_char.rb
facets-2.9.2 src/core/facets/string/each_char.rb
facets-2.9.1 lib/core/facets/string/each_char.rb
facets-2.9.0 lib/core/facets/string/each_char.rb
facets-2.9.0.pre.2 lib/core/facets/string/each_char.rb
facets-2.9.0.pre.1 lib/core/facets/string/each_char.rb