跳至内容 跳至搜索

字符串 屈折变化在 字符串 类上定义了新方法,以转换不同目的的名称。例如,你可以根据类名找出表名。

'ScaleScore'.tableize # => "scale_scores"
方法
A
B
C
D
E
F
H
I
L
M
P
R
S
T
U

常量

BLANK_RE = /\A[[:space:]]*\z/
 
ENCODED_BLANKS = Concurrent::Map.new do |h, enc| h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING) end
 

实例公共方法

acts_like_string?()

启用更可预测的 String 类上的鸭子类型。参见 Object#acts_like?

# File activesupport/lib/active_support/core_ext/string/behavior.rb, line 5
def acts_like_string?
  true
end

at(position)

如果你传递一个整数,则返回该位置的一个字符的子字符串。字符串的第一个字符位于位置 0,下一个位于位置 1,依此类推。如果提供了一个范围,则返回一个包含该范围给定的偏移量处的字符的子字符串。在这两种情况下,如果偏移量为负,则从字符串的末尾开始计数。如果初始偏移量超出字符串,则返回 nil。如果范围的开始大于字符串的末尾,则返回一个空字符串。

str = "hello"
str.at(0)      # => "h"
str.at(1..3)   # => "ell"
str.at(-2)     # => "l"
str.at(-2..-1) # => "lo"
str.at(5)      # => nil
str.at(5..-1)  # => ""

如果给定一个 Regexp,则返回字符串的匹配部分。如果给定一个 String,则如果该字符串出现在字符串中,则返回该字符串。在这两种情况下,如果没有匹配项,则返回 nil

str = "hello"
str.at(/lo/) # => "lo"
str.at(/ol/) # => nil
str.at("lo") # => "lo"
str.at("ol") # => nil
# File activesupport/lib/active_support/core_ext/string/access.rb, line 29
def at(position)
  self[position]
end

blank?()

如果一个字符串为空或只包含空格,则它为空

''.blank?       # => true
'   '.blank?    # => true
"\t\n\r".blank? # => true
' blah '.blank? # => false

支持 Unicode 空格

"\u00a0".blank? # => true

@return [true, false]

# File activesupport/lib/active_support/core_ext/object/blank.rb, line 121
def blank?
  # The regexp that matches blank strings is expensive. For the case of empty
  # strings we can speed up this method (~3.5x) with an empty? call. The
  # penalty for the rest of strings is marginal.
  empty? ||
    begin
      BLANK_RE.match?(self)
    rescue Encoding::CompatibilityError
      ENCODED_BLANKS[self.encoding].match?(self)
    end
end

camelcase(first_letter = :upper)

别名:camelize

camelize(first_letter = :upper)

默认情况下,camelize 将字符串转换为 UpperCamelCase。如果 camelize 的参数设置为 :lower,则 camelize 会生成 lowerCamelCase。

camelize 还会将 ‘/’ 转换为 ‘::’,这对于将路径转换为命名空间很有用。

'active_record'.camelize                # => "ActiveRecord"
'active_record'.camelize(:lower)        # => "activeRecord"
'active_record/errors'.camelize         # => "ActiveRecord::Errors"
'active_record/errors'.camelize(:lower) # => "activeRecord::Errors"

参见 ActiveSupport::Inflector.camelize

别名:camelcase
# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 101
def camelize(first_letter = :upper)
  case first_letter
  when :upper
    ActiveSupport::Inflector.camelize(self, true)
  when :lower
    ActiveSupport::Inflector.camelize(self, false)
  else
    raise ArgumentError, "Invalid option, use either :upper or :lower."
  end
end

classify()

从复数表名创建类名,就像 Rails 对表名到模型所做的那样。请注意,这返回一个字符串,而不是一个类。(要转换为实际类,请将 classifyconstantize 结合使用。)

'ham_and_eggs'.classify # => "HamAndEgg"
'posts'.classify        # => "Post"

参见 ActiveSupport::Inflector.classify

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 239
def classify
  ActiveSupport::Inflector.classify(self)
end

constantize()

constantize 尝试查找字符串中指定名称的已声明常量。当名称不是驼峰式大小写或未初始化时,它会引发 NameError

'Module'.constantize  # => Module
'Class'.constantize   # => Class
'blargle'.constantize # => NameError: wrong constant name blargle

参见 ActiveSupport::Inflector.constantize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 73
def constantize
  ActiveSupport::Inflector.constantize(self)
end

dasherize()

将字符串中的下划线替换为破折号。

'puni_puni'.dasherize # => "puni-puni"

参见 ActiveSupport::Inflector.dasherize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 148
def dasherize
  ActiveSupport::Inflector.dasherize(self)
end

deconstantize()

从字符串中的常量表达式中移除最右边的段。

'Net::HTTP'.deconstantize   # => "Net"
'::Net::HTTP'.deconstantize # => "::Net"
'String'.deconstantize      # => ""
'::String'.deconstantize    # => ""
''.deconstantize            # => ""

参见 ActiveSupport::Inflector.deconstantize

另请参阅 demodulize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 177
def deconstantize
  ActiveSupport::Inflector.deconstantize(self)
end

demodulize()

从字符串中的常量表达式中移除模块部分。

'ActiveSupport::Inflector::Inflections'.demodulize # => "Inflections"
'Inflections'.demodulize                           # => "Inflections"
'::Inflections'.demodulize                         # => "Inflections"
''.demodulize                                      # => ''

参见 ActiveSupport::Inflector.demodulize

另请参阅 deconstantize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 162
def demodulize
  ActiveSupport::Inflector.demodulize(self)
end

downcase_first()

将第一个字符转换为小写。

'If they enjoyed The Matrix'.downcase_first # => "if they enjoyed The Matrix"
'I'.downcase_first                          # => "i"
''.downcase_first                           # => ""

参见 ActiveSupport::Inflector.downcase_first

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 284
def downcase_first
  ActiveSupport::Inflector.downcase_first(self)
end

exclude?(string)

String#include? 的反向。如果字符串不包含其他字符串,则返回 true。

"hello".exclude? "lo" # => false
"hello".exclude? "ol" # => true
"hello".exclude? ?h   # => false
# File activesupport/lib/active_support/core_ext/string/exclude.rb, line 10
def exclude?(string)
  !include?(string)
end

first(limit = 1)

返回第一个字符。如果提供了限制,则返回从字符串开头到达到限制值为止的子字符串。如果给定的限制大于或等于字符串长度,则返回 self 的副本。

str = "hello"
str.first    # => "h"
str.first(1) # => "h"
str.first(2) # => "he"
str.first(0) # => ""
str.first(6) # => "hello"
# File activesupport/lib/active_support/core_ext/string/access.rb, line 78
def first(limit = 1)
  self[0, limit] || raise(ArgumentError, "negative limit")
end

foreign_key(separate_class_name_and_id_with_underscore = true)

从类名创建外键名称。separate_class_name_and_id_with_underscore 设置该方法是否应在名称和“id”之间放置“_”。

'Message'.foreign_key        # => "message_id"
'Message'.foreign_key(false) # => "messageid"
'Admin::Post'.foreign_key    # => "post_id"

请参阅 ActiveSupport::Inflector.foreign_key

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 297
def foreign_key(separate_class_name_and_id_with_underscore = true)
  ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
end

from(position)

返回从给定位置到字符串末尾的子字符串。如果位置为负数,则从字符串末尾开始计数。

str = "hello"
str.from(0)  # => "hello"
str.from(3)  # => "lo"
str.from(-2) # => "lo"

您可以将其与 to 方法混合使用,并执行类似以下操作的有趣操作

str = "hello"
str.from(0).to(-1) # => "hello"
str.from(1).to(-2) # => "ell"
# File activesupport/lib/active_support/core_ext/string/access.rb, line 46
def from(position)
  self[position, length]
end

html_safe()

将字符串标记为受信任的安全。它将被插入 HTML 中,而无需执行额外的转义。您有责任确保字符串不包含任何恶意内容。此方法等效于视图中的 raw 帮助器。建议您使用 sanitize 而不是此方法。它不应在用户输入上调用。

# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 232
def html_safe
  ActiveSupport::SafeBuffer.new(self)
end

humanize(capitalize: true, keep_id_suffix: false)

将第一个单词大写,将下划线转换为空格,并且(默认情况下)如果存在,则去掉尾随的“_id”。与 titleize 一样,这是为了创建漂亮的输出。

可以通过将可选参数 capitalize 设置为 false 来关闭第一个单词的大写。默认情况下,此参数为 true。

可以通过将可选参数 keep_id_suffix 设置为 true 来保留并大写结尾的“_id”。默认情况下,此参数为 false。

'employee_salary'.humanize                    # => "Employee salary"
'author_id'.humanize                          # => "Author"
'author_id'.humanize(capitalize: false)       # => "author"
'_id'.humanize                                # => "Id"
'author_id'.humanize(keep_id_suffix: true)    # => "Author id"

请参阅 ActiveSupport::Inflector.humanize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 262
def humanize(capitalize: true, keep_id_suffix: false)
  ActiveSupport::Inflector.humanize(self, capitalize: capitalize, keep_id_suffix: keep_id_suffix)
end

in_time_zone(zone = ::Time.zone)

如果设置了 Time.zoneTime.zone_default,则将 String 转换为当前时区的 TimeWithZone;否则,将 String 通过 String#to_time 转换为 Time

# File activesupport/lib/active_support/core_ext/string/zones.rb, line 9
def in_time_zone(zone = ::Time.zone)
  if zone
    ::Time.find_zone!(zone).parse(self)
  else
    to_time
  end
end

indent(amount, indent_string = nil, indent_empty_lines = false)

缩进接收器中的行

<<EOS.indent(2)
def some_method
  some_code
end
EOS
# =>
  def some_method
    some_code
  end

第二个参数 indent_string 指定要使用的缩进字符串。默认值为 nil,这告诉方法通过查看第一个缩进行来猜测,如果没有则退回到空格。

"  foo".indent(2)        # => "    foo"
"foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar"
"foo".indent(2, "\t")    # => "\t\tfoo"

虽然 indent_string 通常是一个空格或制表符,但它可以是任何字符串。

第三个参数 indent_empty_lines 是一个标志,表示是否应该缩进空行。默认值为 false。

"foo\n\nbar".indent(2)            # => "  foo\n\n  bar"
"foo\n\nbar".indent(2, nil, true) # => "  foo\n  \n  bar"
# File activesupport/lib/active_support/core_ext/string/indent.rb, line 42
def indent(amount, indent_string = nil, indent_empty_lines = false)
  dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) }
end

indent!(amount, indent_string = nil, indent_empty_lines = false)

indent 相同,只是它就地缩进了接收器。

返回缩进后的字符串,如果没有要缩进的内容,则返回 nil

# File activesupport/lib/active_support/core_ext/string/indent.rb, line 7
def indent!(amount, indent_string = nil, indent_empty_lines = false)
  indent_string = indent_string || self[/^[ \t]/] || " "
  re = indent_empty_lines ? /^/ : /^(?!$)/
  gsub!(re, indent_string * amount)
end

inquiry()

使用 ActiveSupport::StringInquirer 类包装当前字符串,这将为您提供一种更漂亮的方式来测试相等性。

env = 'production'.inquiry
env.production?  # => true
env.development? # => false
# File activesupport/lib/active_support/core_ext/string/inquiry.rb, line 13
def inquiry
  ActiveSupport::StringInquirer.new(self)
end

is_utf8?()

如果字符串具有 utf_8 编码,则返回 true

utf_8_str = "some string".encode "UTF-8"
iso_str = "some string".encode "ISO-8859-1"

utf_8_str.is_utf8? # => true
iso_str.is_utf8?   # => false
# File activesupport/lib/active_support/core_ext/string/multibyte.rb, line 48
def is_utf8?
  case encoding
  when Encoding::UTF_8, Encoding::US_ASCII
    valid_encoding?
  when Encoding::ASCII_8BIT
    dup.force_encoding(Encoding::UTF_8).valid_encoding?
  else
    false
  end
end

last(limit = 1)

返回字符串的最后一个字符。如果提供了限制,则从字符串的末尾返回一个子字符串,直到达到限制值(向后计数)。如果给定的限制大于或等于字符串长度,则返回 self 的副本。

str = "hello"
str.last    # => "o"
str.last(1) # => "o"
str.last(2) # => "lo"
str.last(0) # => ""
str.last(6) # => "hello"
# File activesupport/lib/active_support/core_ext/string/access.rb, line 92
def last(limit = 1)
  self[[length - limit, 0].max, limit] || raise(ArgumentError, "negative limit")
end

mb_chars()

多字节代理

mb_chars 是字符串方法的多字节安全代理。

它创建并返回 ActiveSupport::Multibyte::Chars 类的实例,该实例封装了原始字符串。在这个代理类上定义了所有 String 方法的 Unicode 安全版本。如果代理类没有响应某个方法,则会将该方法转发到封装的字符串。

>> "lj".mb_chars.upcase.to_s
=> "LJ"

注意:Ruby 2.4 及更高版本支持本机 Unicode 大小写映射

>> "lj".upcase
=> "LJ"

Method 链式

Chars 代理中的所有通常返回字符串的方法将返回一个 Chars 对象。这允许在这些方法的任何结果上进行方法链接。

name.mb_chars.reverse.length # => 12

互操作性和配置

Chars 对象尝试尽可能地与 String 对象互换:对 String 和 Char 之间进行排序和比较的工作方式符合预期。bang! 方法更改 Chars 对象中的内部字符串表示形式。互操作性问题可以通过 to_s 调用轻松解决。

有关 Chars 代理中定义的方法的更多信息,请参阅 ActiveSupport::Multibyte::Chars。有关如何更改默认 Multibyte 行为的信息,请参阅 ActiveSupport::Multibyte

# File activesupport/lib/active_support/core_ext/string/multibyte.rb, line 37
def mb_chars
  ActiveSupport::Multibyte.proxy_class.new(self)
end

parameterize(separator: "-", preserve_case: false, locale: nil)

替换字符串中的特殊字符,以便将其用作“漂亮”URL 的一部分。

如果指定了可选参数 locale,则该单词将作为该语言的单词进行参数化。默认情况下,此参数设置为 nil,它将使用已配置的 I18n.locale

class Person
  def to_param
    "#{id}-#{name.parameterize}"
  end
end

@person = Person.find(1)
# => #<Person id: 1, name: "Donald E. Knuth">

<%= link_to(@person.name, person_path) %>
# => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>

要保留字符串中字符的大小写,请使用 preserve_case 参数。

class Person
  def to_param
    "#{id}-#{name.parameterize(preserve_case: true)}"
  end
end

@person = Person.find(1)
# => #<Person id: 1, name: "Donald E. Knuth">

<%= link_to(@person.name, person_path) %>
# => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>

请参阅 ActiveSupport::Inflector.parameterize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 215
def parameterize(separator: "-", preserve_case: false, locale: nil)
  ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case, locale: locale)
end

pluralize(count = nil, locale = :en)

返回字符串中单词的复数形式。

如果指定了可选参数 count,则如果 count == 1,则将返回单数形式。对于 count 的任何其他值,都将返回复数形式。

如果指定了可选参数 locale,则该单词将作为该语言的单词进行复数化。默认情况下,此参数设置为 :en。您必须为英语以外的语言定义自己的变格规则。

'post'.pluralize             # => "posts"
'octopus'.pluralize          # => "octopi"
'sheep'.pluralize            # => "sheep"
'words'.pluralize            # => "words"
'the blue mailman'.pluralize # => "the blue mailmen"
'CamelOctopus'.pluralize     # => "CamelOctopi"
'apple'.pluralize(1)         # => "apple"
'apple'.pluralize(2)         # => "apples"
'ley'.pluralize(:es)         # => "leyes"
'ley'.pluralize(1, :es)      # => "ley"

请参阅 ActiveSupport::Inflector.pluralize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 35
def pluralize(count = nil, locale = :en)
  locale = count if count.is_a?(Symbol)
  if count == 1
    dup
  else
    ActiveSupport::Inflector.pluralize(self, locale)
  end
end

remove(*patterns)

返回一个新字符串,其中删除了所有模式的出现。

str = "foo bar test"
str.remove(" test")                 # => "foo bar"
str.remove(" test", /bar/)          # => "foo "
str                                 # => "foo bar test"
# File activesupport/lib/active_support/core_ext/string/filters.rb, line 32
def remove(*patterns)
  dup.remove!(*patterns)
end

remove!(*patterns)

通过删除所有模式的出现来更改字符串。

str = "foo bar test"
str.remove!(" test", /bar/)         # => "foo "
str                                 # => "foo "
# File activesupport/lib/active_support/core_ext/string/filters.rb, line 40
def remove!(*patterns)
  patterns.each do |pattern|
    gsub! pattern, ""
  end

  self
end

safe_constantize()

safe_constantize 尝试查找字符串中指定的名称的已声明常量。当名称不是驼峰式或未初始化时,它返回 nil

'Module'.safe_constantize  # => Module
'Class'.safe_constantize   # => Class
'blargle'.safe_constantize # => nil

请参见 ActiveSupport::Inflector.safe_constantize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 86
def safe_constantize
  ActiveSupport::Inflector.safe_constantize(self)
end

singularize(locale = :en)

pluralize 的反向,返回字符串中单词的单数形式。

如果指定了可选参数 locale,则该单词将作为该语言的单词进行单数化。默认情况下,此参数设置为 :en。您必须为英语以外的语言定义自己的变格规则。

'posts'.singularize            # => "post"
'octopi'.singularize           # => "octopus"
'sheep'.singularize            # => "sheep"
'word'.singularize             # => "word"
'the blue mailmen'.singularize # => "the blue mailman"
'CamelOctopi'.singularize      # => "CamelOctopus"
'leyes'.singularize(:es)       # => "ley"

请参见 ActiveSupport::Inflector.singularize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 60
def singularize(locale = :en)
  ActiveSupport::Inflector.singularize(self, locale)
end

squish()

返回字符串,首先删除字符串两端的空格,然后将剩余的连续空格组转换为一个空格。

请注意,它处理 ASCII 和 Unicode 空格。

%{ Multi-line
   string }.squish                   # => "Multi-line string"
" foo   bar    \n   \t   boo".squish # => "foo bar boo"
# File activesupport/lib/active_support/core_ext/string/filters.rb, line 13
def squish
  dup.squish!
end

squish!()

执行破坏性压缩。请参阅 String#squish

str = " foo   bar    \n   \t   boo"
str.squish!                         # => "foo bar boo"
str                                 # => "foo bar boo"
# File activesupport/lib/active_support/core_ext/string/filters.rb, line 21
def squish!
  gsub!(/[[:space:]]+/, " ")
  strip!
  self
end

strip_heredoc()

去除 heredocs 中的缩进。

例如,在中

if options[:usage]
  puts <<-USAGE.strip_heredoc
    This command does such and such.

    Supported options are:
      -h         This message
      ...
  USAGE
end

用户将看到与左边界对齐的使用情况消息。

从技术上讲,它会在整个字符串中查找缩进最少的非空行,并删除该数量的前导空格。

# File activesupport/lib/active_support/core_ext/string/strip.rb, line 22
def strip_heredoc
  gsub(/^#{scan(/^[ \t]*(?=\S)/).min}/, "").tap do |stripped|
    stripped.freeze if frozen?
  end
end

tableize()

创建表名,就像 Rails 对模型到表名所做的那样。此方法对字符串中的最后一个单词使用 pluralize 方法。

'RawScaledScorer'.tableize # => "raw_scaled_scorers"
'ham_and_egg'.tableize     # => "ham_and_eggs"
'fancyCategory'.tableize   # => "fancy_categories"

请参阅 ActiveSupport::Inflector.tableize

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 227
def tableize
  ActiveSupport::Inflector.tableize(self)
end

titlecase(keep_id_suffix: false)

别名:titleize

titleize(keep_id_suffix: false)

将字符串中的所有单词大写,并替换一些字符以创建更好看的标题。titleize 用于创建漂亮的输出。它不用于 Rails 内部。

可以通过将可选参数 keep_id_suffix 设置为 true 来保留并大写结尾的“_id”、“Id”等。默认情况下,此参数为 false。

'man from the boondocks'.titleize                       # => "Man From The Boondocks"
'x-men: the last stand'.titleize                        # => "X Men: The Last Stand"
'string_ending_with_id'.titleize(keep_id_suffix: true)  # => "String Ending With Id"

请参阅 ActiveSupport::Inflector.titleize

别名:titlecase
# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 126
def titleize(keep_id_suffix: false)
  ActiveSupport::Inflector.titleize(self, keep_id_suffix: keep_id_suffix)
end

to(position)

从字符串开头返回一个子字符串到给定位置。如果位置为负,则从字符串末尾开始计数。

str = "hello"
str.to(0)  # => "h"
str.to(3)  # => "hell"
str.to(-2) # => "hell"

您可以将它与 from 方法混合使用,并执行诸如

str = "hello"
str.from(0).to(-1) # => "hello"
str.from(1).to(-2) # => "ell"
# File activesupport/lib/active_support/core_ext/string/access.rb, line 63
def to(position)
  position += size if position < 0
  self[0, position + 1] || +""
end

to_date()

将字符串转换为 Date 值。

"1-1-2012".to_date   # => Sun, 01 Jan 2012
"01/01/2012".to_date # => Sun, 01 Jan 2012
"2012-12-13".to_date # => Thu, 13 Dec 2012
"12/13/2012".to_date # => ArgumentError: invalid date
# File activesupport/lib/active_support/core_ext/string/conversions.rb, line 47
def to_date
  ::Date.parse(self, false) unless blank?
end

to_datetime()

将字符串转换为 DateTime 值。

"1-1-2012".to_datetime            # => Sun, 01 Jan 2012 00:00:00 +0000
"01/01/2012 23:59:59".to_datetime # => Sun, 01 Jan 2012 23:59:59 +0000
"2012-12-13 12:50".to_datetime    # => Thu, 13 Dec 2012 12:50:00 +0000
"12/13/2012".to_datetime          # => ArgumentError: invalid date
# File activesupport/lib/active_support/core_ext/string/conversions.rb, line 57
def to_datetime
  ::DateTime.parse(self, false) unless blank?
end

to_time(form = :local)

将字符串转换为 Time 值。form 可以是 :utc:local(默认 :local)。

时间使用 Time.parse 方法进行解析。如果 form:local,则时间为系统时区。如果日期部分缺失,则使用当前日期,如果时间部分缺失,则假定为 00:00:00。

"13-12-2012".to_time               # => 2012-12-13 00:00:00 +0100
"06:12".to_time                    # => 2012-12-13 06:12:00 +0100
"2012-12-13 06:12".to_time         # => 2012-12-13 06:12:00 +0100
"2012-12-13T06:12".to_time         # => 2012-12-13 06:12:00 +0100
"2012-12-13T06:12".to_time(:utc)   # => 2012-12-13 06:12:00 UTC
"12/13/2012".to_time               # => ArgumentError: argument out of range
"1604326192".to_time               # => ArgumentError: argument out of range
# File activesupport/lib/active_support/core_ext/string/conversions.rb, line 22
def to_time(form = :local)
  parts = Date._parse(self, false)
  used_keys = %i(year mon mday hour min sec sec_fraction offset)
  return if (parts.keys & used_keys).empty?

  now = Time.now
  time = Time.new(
    parts.fetch(:year, now.year),
    parts.fetch(:mon, now.month),
    parts.fetch(:mday, now.day),
    parts.fetch(:hour, 0),
    parts.fetch(:min, 0),
    parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
    parts.fetch(:offset, form == :utc ? 0 : nil)
  )

  form == :utc ? time.utc : time.to_time
end

truncate(truncate_to, options = {})

如果 text 长度大于 truncate_to,则将给定的 text 截断至长度 truncate_to

'Once upon a time in a world far far away'.truncate(27)
# => "Once upon a time in a wo..."

传递字符串或正则表达式 :separator 以在自然中断处截断 text

'Once upon a time in a world far far away'.truncate(27, separator: ' ')
# => "Once upon a time in a..."

'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
# => "Once upon a time in a..."

最后一个字符将被替换为 :omission 字符串(默认为 “…”)。除非 text:omission 都长于 truncate_to,否则总长度不会超过 truncate_to

'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
# => "And they f... (continued)"

'And they found that many people were sleeping better.'.truncate(4, omission: '... (continued)')
# => "... (continued)"
# File activesupport/lib/active_support/core_ext/string/filters.rb, line 70
def truncate(truncate_to, options = {})
  return dup unless length > truncate_to

  omission = options[:omission] || "..."
  length_with_room_for_omission = truncate_to - omission.length
  stop = \
    if options[:separator]
      rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
    else
      length_with_room_for_omission
    end

  +"#{self[0, stop]}#{omission}"
end

truncate_bytes(truncate_to, omission: "…")

text 截断至最多 truncate_to 字节长度,不会通过分割多字节字符或截断组合字符来破坏字符串编码或破坏字形簇(“感知字符”)。

>> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".size
=> 20
>> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".bytesize
=> 80
>> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".truncate_bytes(20)
=> "🔪🔪🔪🔪…"

截断文本以 :omission 字符串结尾,默认为 “…”,总长度不超过 truncate_to

:omission 的字节大小超过 truncate_to 时,引发 ArgumentError

# File activesupport/lib/active_support/core_ext/string/filters.rb, line 101
def truncate_bytes(truncate_to, omission: "…")
  omission ||= ""

  case
  when bytesize <= truncate_to
    dup
  when omission.bytesize > truncate_to
    raise ArgumentError, "Omission #{omission.inspect} is #{omission.bytesize}, larger than the truncation length of #{truncate_to} bytes"
  when omission.bytesize == truncate_to
    omission.dup
  else
    self.class.new.tap do |cut|
      cut_at = truncate_to - omission.bytesize

      each_grapheme_cluster do |grapheme|
        if cut.bytesize + grapheme.bytesize <= cut_at
          cut << grapheme
        else
          break
        end
      end

      cut << omission
    end
  end
end

truncate_words(words_count, options = {})

在给定的单词数(words_count)后截断给定的 text

'Once upon a time in a world far far away'.truncate_words(4)
# => "Once upon a time..."

传递字符串或正则表达式 :separator 以指定不同的单词分隔符

'Once<br>upon<br>a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>')
# => "Once<br>upon<br>a<br>time<br>in..."

最后一个字符将被替换为 :omission 字符串(默认为 “…”)

'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')
# => "And they found that many... (continued)"
# File activesupport/lib/active_support/core_ext/string/filters.rb, line 142
def truncate_words(words_count, options = {})
  sep = options[:separator] || /\s+/
  sep = Regexp.escape(sep.to_s) unless Regexp === sep
  if self =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
    $1 + (options[:omission] || "...")
  else
    dup
  end
end

underscore()

camelize 的反向操作。从字符串中的表达式生成下划线分隔的小写形式。

underscore 还会将“::”更改为“/”,以将命名空间转换为路径。

'ActiveModel'.underscore         # => "active_model"
'ActiveModel::Errors'.underscore # => "active_model/errors"

请参阅 ActiveSupport::Inflector.underscore

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 139
def underscore
  ActiveSupport::Inflector.underscore(self)
end

upcase_first()

将第一个字符转换为大写。

'what a Lovely Day'.upcase_first # => "What a Lovely Day"
'w'.upcase_first                 # => "W"
''.upcase_first                  # => ""

请参阅 ActiveSupport::Inflector.upcase_first

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 273
def upcase_first
  ActiveSupport::Inflector.upcase_first(self)
end