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
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
new
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::Multibyte/ActiveSupport::Multibyte::Chars

new(string) Class Public methods Creates a new

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

capitalize() Instance Public methods Converts the first character to uppercase

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

downcase() Instance Public methods Converts characters in the string to lowercase

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

tidy_bytes(force = false) Instance Public methods Replaces all ISO-8859-1 or

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

titlecase() Instance Public methods Alias for:

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

grapheme_length() Instance Public methods Returns the number of grapheme clusters

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

upcase() Instance Public methods Converts characters in the string to uppercase

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

reverse() Instance Public methods Reverses all characters in the string.

2025-01-10 15:47:30