Class ActiveSupport::Multibyte::Chars
In: vendor/rails/activesupport/lib/active_support/multibyte/chars.rb
Parent: Object

Chars enables you to work transparently with multibyte encodings 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 chars method. Methods which would normally return a String object now return a Chars object so methods can be chained.

  "The Perfect String  ".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".chars.downcase.to_s

The actual operations on the string are delegated to handlers. Theoretically handlers can be implemented for any encoding, but the default handler handles UTF-8. This handler is set during initialization, if you want to use you own handler, you can set it on the Chars class. Look at the UTF8Handler source for an example how to implement your own handler. If you your own handler to work on anything but UTF-8 you probably also want to override Chars#handler.

  ActiveSupport::Multibyte::Chars.handler = MyHandler

Note that a few methods are defined on Chars instead of the handler because they are defined on Object or Kernel and method_missing can‘t catch them.

Methods

<=>   =~   gsub   handler   handler=   method_missing   new   respond_to?   split   to_str  

Included Modules

Comparable

External Aliases

string -> to_s

Attributes

string  [R] 

Public Class methods

Set the handler class for the Char objects.

[Source]

     # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 104
104:     def self.handler=(klass)
105:       @@handler = klass
106:     end

Create a new Chars instance.

[Source]

    # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 51
51:     def initialize(str)
52:       @string = str.respond_to?(:string) ? str.string : str
53:     end

Public Instance methods

Returns -1, 0 or +1 depending on whether the Chars object is to be sorted before, equal or after the object on the right side of the operation. It accepts any object that implements to_s. See String.<=> for more details.

[Source]

    # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 58
58:     def <=>(other); @string <=> other.to_s; end

Like String.=~ only it returns the character offset (in codepoints) instead of the byte offset.

[Source]

    # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 70
70:     def =~(other)
71:       handler.translate_offset(@string, @string =~ other)
72:     end

Gsub works exactly the same as gsub on a normal string.

[Source]

    # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 67
67:     def gsub(*a, &b); @string.gsub(*a, &b).chars; end

Returns the proper handler for the contained string depending on $KCODE and the encoding of the string. This method is used internally to always redirect messages to the proper classes depending on the context.

[Source]

     # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 110
110:     def handler
111:       if utf8_pragma?
112:         @@handler
113:       else
114:         ActiveSupport::Multibyte::Handlers::PassthruHandler
115:       end
116:     end

Try to forward all undefined methods to the handler, when a method is not defined on the handler, send it to the contained string. Method_missing is also responsible for making the bang! methods destructive.

[Source]

     # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 76
 76:     def method_missing(m, *a, &b)
 77:       begin
 78:         # Simulate methods with a ! at the end because we can't touch the enclosed string from the handlers.
 79:         if m.to_s =~ /^(.*)\!$/ && handler.respond_to?($1)
 80:           result = handler.send($1, @string, *a, &b)
 81:           if result == @string
 82:             result = nil
 83:           else
 84:             @string.replace result
 85:           end
 86:         elsif handler.respond_to?(m)
 87:           result = handler.send(m, @string, *a, &b)
 88:         else
 89:           result = @string.send(m, *a, &b)
 90:         end
 91:       rescue Handlers::EncodingError
 92:         @string.replace handler.tidy_bytes(@string)
 93:         retry
 94:       end
 95:       
 96:       if result.kind_of?(String)
 97:         result.chars
 98:       else
 99:         result
100:       end
101:     end

Make duck-typing with String possible

[Source]

    # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 45
45:     def respond_to?(method)
46:       super || @string.respond_to?(method) || handler.respond_to?(method) ||
47:         (method.to_s =~ /(.*)!/ && handler.respond_to?($1)) || false
48:     end

Works just like String#split, with the exception that the items in the resulting list are Chars instances instead of String. This makes chaining methods easier.

[Source]

    # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 62
62:     def split(*args)
63:       @string.split(*args).map { |i| i.chars }
64:     end

The magic method to make String and Chars comparable

[Source]

    # File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 38
38:     def to_str
39:       # Using any other ways of overriding the String itself will lead you all the way from infinite loops to
40:       # core dumps. Don't go there.
41:       @string
42:     end

[Validate]