跳至内容 跳至搜索

String 屈折定义了在 String 类上的新方法,用于将名称转换为不同的用途。例如,您可以从类的名称中找出表的名称。

'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?()

在类似字符串的类上启用更可预测的鸭子类型。参见 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 153
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 为表名到模型所做的那样。请注意,这将返回一个字符串,而不是一个类。(要转换为实际类,请在 classify 之后使用 constantize。)

'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)

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

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 而不是此方法。它永远不应该在用户输入上调用。此方法等效于视图中的 raw 帮助程序。建议您使用 sanitize 而不是此方法。它永远不应该在用户输入上调用。

# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 225
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#to_timeString 转换为 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"

方法链接

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

name.mb_chars.reverse.length # => 12

互操作性和配置

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

有关在 Chars 代理上定义的方法的更多信息,请参阅 ActiveSupport::Multibyte::Chars。有关如何更改默认多字节行为的信息,请参阅 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 尝试使用字符串中指定的名称查找已声明的常量。当名称不在 CamelCase 中或未初始化时,它返回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.intersect?(used_keys)

  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字符串(默认为“...”)。总长度不会超过truncate_to,除非text:omission都比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.force_encoding(encoding).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