跳到內文 跳到搜尋

Active Support Multibyte Chars

Chars 能讓您使用 Ruby String 類別透明地使用 UTF-8 編碼,而不必具備深入瞭解此編碼的知識。 Chars 物件在初始化時會接收字串,並以編碼安全的方式代理 String 方法。所有 String 方法也都會在此代理項中實作。

String 方法會透過 Chars 物件代理,並能透過 mb_chars 方法加以存取。原本會回傳 String 物件的方法現會回傳 Chars 物件,以便鏈結這些方法。

'The Perfect String  '.mb_chars.downcase.strip
# => #<ActiveSupport::Multibyte::Chars:0x007fdc434ccc10 @wrapped_string="the perfect string">

只要不進行明確的類別檢查, Chars 物件就能與 String 物件完美互換。如果某些方法確實會明確檢查類別,請在將 chars 物件傳送給這些方法之前呼叫 to_s

bad.explicit_checking_method 'T'.mb_chars.downcase.to_s

預設的 Chars 實作預設假設字串的編碼是 UTF-8,如果您想要處理不同的編碼,則可以撰寫自己的多位元組字串處理常式,並透過 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
方法
C
D
G
L
M
N
R
S
T
已包含的模組

屬性

[R] to_s
[R] to_str
[R] wrapped_string

類別公開方法

new(string)

透過包裝 字串來建立新的 Chars 實例。

# File activesupport/lib/active_support/multibyte/chars.rb, line 56
def initialize(string)
  @wrapped_string = string
  @wrapped_string.force_encoding(Encoding::UTF_8) unless @wrapped_string.frozen?
end

實例公開方法

compose()

對所有字元進行組合。

'é'.length                       # => 1
'é'.mb_chars.compose.to_s.length # => 1
# File activesupport/lib/active_support/multibyte/chars.rb, line 140
def compose
  chars(Unicode.compose(@wrapped_string.codepoints.to_a).pack("U*"))
end

decompose()

對所有字元執行正規分解。

'é'.length                         # => 1
'é'.mb_chars.decompose.to_s.length # => 2
# File activesupport/lib/active_support/multibyte/chars.rb, line 132
def decompose
  chars(Unicode.decompose(:canonical, @wrapped_string.codepoints.to_a).pack("U*"))
end

grapheme_length()

傳回字串中的書寫體叢集數量。

'क्षि'.mb_chars.length   # => 4
'क्षि'.mb_chars.grapheme_length # => 2
# File activesupport/lib/active_support/multibyte/chars.rb, line 148
def grapheme_length
  @wrapped_string.grapheme_clusters.length
end

limit(limit)

限制字串的位元組大小在不分隔字元的狀態下。當儲存字串的空間受限時,可以使用此方法。

'こんにちは'.mb_chars.limit(7).to_s # => "こん"
# File activesupport/lib/active_support/multibyte/chars.rb, line 115
def limit(limit)
  chars(@wrapped_string.truncate_bytes(limit, omission: nil))
end

method_missing(method, ...)

將所有未定義的方法轉送至封裝的字串。

# File activesupport/lib/active_support/multibyte/chars.rb, line 62
def method_missing(method, ...)
  result = @wrapped_string.__send__(method, ...)
  if method.end_with?("!")
    self if result
  else
    result.kind_of?(String) ? chars(result) : result
  end
end

respond_to_missing?(method, include_private)

如果 obj 會回應給定方法則傳回 true。如果第二個參數選項評估為 true,則在搜尋中包含私人方法。

# File activesupport/lib/active_support/multibyte/chars.rb, line 74
def respond_to_missing?(method, include_private)
  @wrapped_string.respond_to?(method, include_private)
end

reverse()

反轉字串中的所有字元。

'Café'.mb_chars.reverse.to_s # => 'éfaC'
# File activesupport/lib/active_support/multibyte/chars.rb, line 106
def reverse
  chars(@wrapped_string.grapheme_clusters.reverse.join)
end

slice!(*args)

如同 String#slice!,但傳回 Chars 的實例,或是在字串未修改時傳回 nil。如果給定的範圍超出界線,則字串不會被修改

string = 'Welcome'
string.mb_chars.slice!(3)    # => #<ActiveSupport::Multibyte::Chars:0x000000038109b8 @wrapped_string="c">
string # => 'Welome'
string.mb_chars.slice!(0..3) # => #<ActiveSupport::Multibyte::Chars:0x00000002eb80a0 @wrapped_string="Welo">
string # => 'me'
# File activesupport/lib/active_support/multibyte/chars.rb, line 96
def slice!(*args)
  string_sliced = @wrapped_string.slice!(*args)
  if string_sliced
    chars(string_sliced)
  end
end

split(*args)

正如同 String#split,但有例外情形:在 resulting list 中的項目是 Chars 實例,而不是 String。這讓串連方法變得更容易。

'Café périferôl'.mb_chars.split(/é/).map { |part| part.upcase.to_s } # => ["CAF", " P", "RIFERÔL"]
# File activesupport/lib/active_support/multibyte/chars.rb, line 83
def split(*args)
  @wrapped_string.split(*args).map { |i| self.class.new(i) }
end

tidy_bytes(force = false)

用 UTF-8 等效的字元取代所有 ISO-8859-1 或 CP1252 字元,產生有效的 UTF-8 字串。

傳入 true 會強制整理所有位元組,假設字串的編碼完全是 CP1252 或 ISO-8859-1。

# File activesupport/lib/active_support/multibyte/chars.rb, line 157
def tidy_bytes(force = false)
  chars(Unicode.tidy_bytes(@wrapped_string, force))
end

titlecase()

別名為: titleize

titleize()

儘可能將每個單字的第一個字母大寫。

"ÉL QUE SE ENTERÓ".mb_chars.titleize.to_s    # => "Él Que Se Enteró"
"日本語".mb_chars.titleize.to_s               # => "日本語"
另別名為: titlecase