aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/string/iterators.rb
blob: 472de7bc244b81572f0697ce3c1c15dd25ac78a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
unless '1.9'.respond_to?(:each_char)
  autoload :StringScanner, 'strscan' unless defined? :StringScanner

  class String
    # 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
      while c = scanner.scan(char)
        yield c
      end
    end
  end
end