These methods depend on the current value of the $KCODE global variable, which
determines the character encoding in use. $KCODE can take on the following values.
In Rails 1.2, $KCODE is automatically set to u, enabling the multibyte operations.
??? String#chars returns an instance of ActiveSupport::Multibyte::Chars, which proxies
for the String class. Chars defines methods that work properly under Unicode (assuming
the proper $KCODE setting), and proxies methods it doesn??™t know about to String:
str =
$KCODE = "a"
str # => "\344\273\212\346\227\245\343\201\257\344\270\226\347\225\214"
str.length # => 15
str.chars.class # => ActiveSupport::Multibyte::Chars
str.chars.length # => 15
$KCODE = "u"
str # =>
str.length # => 15
str.chars.length # => 5
String#each_char yields each character in turn to the block:
$KCODE = "u"
.each_char {|c| puts c}
# >>
# >>
# >>
# >>
# >>
$KCODE Encoding
e, E EUC
s, S Shift-JIS
u, U UTF-8
a, A, n, N ASCII (default)
Symbol#to_proc
74 | Chapter 2: ActiveSupport and RailTies
Miscellaneous methods core_ext/string/access.rb, core_ext/string/conversions.rb,
core_ext/string/starts_ends_with.rb
??? String#at(position) returns the character at the specified position. This is an easy way
around the fact that Ruby??™s String#[] returns a character code rather than a string in
Ruby 1.8:
"asdf"[0] # => 97
"asdf"[0] == ?a # => true
"asdf".
Pages:
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120