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.

1
'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.

1
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.

1
2
3
4
5
6
7
8
9
10
11
class CharsForUTF32
  def size
    @wrapped_string.size / 4
  end
 
  def self.accepts?(string)
    string.length % 4 == 0
  end
end
 
ActiveSupport::Multibyte.proxy_class = CharsForUTF32
method_missing
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

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

2025-01-10 15:47:30
split
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

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

2025-01-10 15:47:30
consumes?
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

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

2025-01-10 15:47:30
normalize
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

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

2025-01-10 15:47:30
respond_to_missing?
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

respond_to_missing?(method, include_private) Instance Public methods Returns

2025-01-10 15:47:30
swapcase
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

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

2025-01-10 15:47:30
slice!
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

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

2025-01-10 15:47:30
decompose
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

decompose() Instance Public methods Performs canonical decomposition on all

2025-01-10 15:47:30
titleize
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

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

2025-01-10 15:47:30
compose
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

compose() Instance Public methods Performs composition on all the characters

2025-01-10 15:47:30