Type:
Class

Chars enables you to work transparently with UTF-8 encoding in the Ruby String class without having extensive knowledge about the encoding. A Chars object accepts a string upon initialization and proxies String methods in an encoding safe manner. All the normal String methods are also implemented on the proxy.

String methods are proxied through the Chars object, and can be accessed through the mb_chars method. Methods which would normally return a String object now return a Chars object so methods can be chained.

'The Perfect String  '.mb_chars.downcase.strip.normalize # => "the perfect string"

Chars objects are perfectly interchangeable with String objects as long as no explicit class checks are made. If certain methods do explicitly check the class, call to_s before you pass chars objects to them.

bad.explicit_checking_method 'T'.mb_chars.downcase.to_s

The default Chars implementation assumes that the encoding of the string is UTF-8, if you want to handle different encodings you can write your own multibyte string handler and configure it through ActiveSupport::Multibyte.proxy_class.

class CharsForUTF32
  def size
    @wrapped_string.size / 4
  end

  def self.accepts?(string)
    string.length % 4 == 0
  end
end

ActiveSupport::Multibyte.proxy_class = CharsForUTF32
normalize

normalize(form = nil) Instance Public methods Returns the KC normalization of

2015-06-20 00:00:00
method_missing

method_missing(method, *args, &block) Instance Public methods Forward all

2015-06-20 00:00:00
consumes?

consumes?(string) Class Public methods Returns true when the proxy

2015-06-20 00:00:00
split

split(*args) Instance Public methods Works just like String#split

2015-06-20 00:00:00
decompose

decompose() Instance Public methods Performs canonical decomposition on all

2015-06-20 00:00:00
slice!

slice!(*args) Instance Public methods Works like like String#slice!

2015-06-20 00:00:00
titleize

titleize() Instance Public methods Capitalizes the first letter of every word

2015-06-20 00:00:00
limit

limit(limit) Instance Public methods Limits the byte size of the string to a

2015-06-20 00:00:00
respond_to_missing?

respond_to_missing?(method, include_private) Instance Public methods Returns

2015-06-20 00:00:00
swapcase

swapcase() Instance Public methods Converts characters in the string to the

2015-06-20 00:00:00