Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
String

String | 73


  • String#tableize converts class names to table names (as in ActiveRecord).
    String#classify converts table names to class names.
    "ProductCategory".tableize # => "product_categories"
    "product_categories".classify # => "ProductCategory"

  • String#constantizetries to interpret the given string as a constant name. This is use-
    ful for looking up classes returned fromString#classify.
    "ProductCategory".constantize rescue "no class" # => "no class"
    class ProductCategory; end
    "ProductCategory".constantize # => ProductCategory


String i18n core_ext/string/unicode.rb,core_ext/string/iterators.rb


These files provide interfaces to ActiveSupport::Multibyte for support for multibyte charac-
ters. These methods depend on the current value of the$KCODEglobal variable, which
determines the character encoding in use.$KCODE can take on the following values.


In Rails 1.2,$KCODE is automatically set tou, enabling the multibyte operations.



  • String#charsreturns an instance ofActiveSupport::Multibyte::Chars, which proxies
    for theStringclass.Charsdefines methods that work properly under Unicode (assum-
    ing the proper$KCODE setting), and proxies methods it doesn’t know about toString:
    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)
Free download pdf