Class: String

Inherits:
Object show all
Defined in:
runtime/ext/string.rb,
runtime/ext/date.rbs,
runtime/ext/string.rbs,
runtime/ext/date.rb

Overview

String — the text helpers.

Instance Method Summary collapse

  • #camelize(mode = :upper) ⇒ String

    "host_info" → "HostInfo", or "hostInfo" with mode: :lower: underscore-separated words capitalized, a / read as a namespace separator ("a/b" → "A::B").

  • #humanize ⇒ String

    "foo_bar" → "Foo bar": underscores to spaces, a trailing " id" dropped, first letter upcased.

  • #parameterize ⇒ String

    this text as a URL slug: downcased, with every run of characters outside a-z, 0-9 and "_" folded into a single "-", runs of "-" collapsed, and "-" trimmed off both ends.

  • #pluralize(count) ⇒ String

    the plural of this word, or the word unchanged when count == 1: the word a count labels, singular for one item and plural for every other count.

  • #squish ⇒ String

    whitespace runs collapse to one space and the ends strip: the shape <<~SQL.squish produces.

  • #titleize ⇒ String

    "foo_bar" → "Foo Bar": each word's first letter upcased.

  • #to_date ⇒ Date?

    "2024-01-02" → the civil date a stored DATE column holds.

  • #to_time ⇒ Time?

    "2024-01-02 03:04:05.000006" → the instant, in the app's configured zone: a stored datetime column read back.

  • #underscore ⇒ String

    "FooBar" → "foo_bar": camel boundaries split, - reads as _, the result downcased.

Instance Method Details

#camelize(mode = :upper) ⇒ String

"host_info" → "HostInfo", or "hostInfo" with mode: :lower: underscore-separated words capitalized, a / read as a namespace separator ("a/b" → "A::B"). The shape JSON keys need when a payload is camelCase.

Examples:

"host_info".camelize           # => "HostInfo"
"host_info".camelize(:lower)   # => "hostInfo"

Parameters:

  • mode (Symbol) (defaults to: :upper) —

    :upper capitalizes the leading word, :lower decamels it

Returns:

  • (String) —

    the camelCase form of this text

Raises:

  • (ArgumentError) —

    mode is neither :upper nor :lower



64
# File 'runtime/ext/string.rb', line 64

def camelize: (?Symbol mode) -> String

#humanize ⇒ String

"foo_bar" → "Foo bar": underscores to spaces, a trailing " id" dropped, first letter upcased.

Returns:

  • (String) —

    this text in sentence case



101
# File 'runtime/ext/string.rb', line 101

def humanize: () -> String

#parameterize ⇒ String

this text as a URL slug: downcased, with every run of characters outside a-z, 0-9 and "_" folded into a single "-", runs of "-" collapsed, and "-" trimmed off both ends.

Letters outside ASCII are dropped rather than transliterated, so a non-Latin word parameterizes to an empty string.

Examples:

"Hello World".parameterize  # => "hello-world"
"a  b".parameterize         # => "a-b"

Returns:

  • (String) —

    the slug form of this text



175
# File 'runtime/ext/string.rb', line 175

def parameterize: () -> String

#pluralize(count) ⇒ String

the plural of this word, or the word unchanged when count == 1: the word a count labels, singular for one item and plural for every other count.

The suffix rules are the regular English ones, not the full irregular tables: a consonant before "y" turns to "ies" ("category"), "x", "z", "ch" and "sh" take "es" ("box", "church"), and a word that already ends in "s" keeps its form ("posts").

Examples:

"post".pluralize(1)      # => "post"
"post".pluralize(2)      # => "posts"
"category".pluralize(3)  # => "categories"

Parameters:

  • count (Integer) —

    how many items the word labels

Returns:

  • (String) —

    the word unchanged for one item, its plural otherwise



141
# File 'runtime/ext/string.rb', line 141

def pluralize: (Integer count) -> String

#squish ⇒ String

whitespace runs collapse to one space and the ends strip: the shape <<~SQL.squish produces. The scan is spelled out because a regex is not a form the compiler lowers.

Returns:

  • (String) —

    this text with whitespace runs folded to one space and the ends stripped



9
# File 'runtime/ext/string.rb', line 9

def squish: () -> String

#titleize ⇒ String

"foo_bar" → "Foo Bar": each word's first letter upcased.

Returns:

  • (String) —

    this text in title case



112
# File 'runtime/ext/string.rb', line 112

def titleize: () -> String

#to_date ⇒ Date?

"2024-01-02" → the civil date a stored DATE column holds. The parse is the stored form's fixed width, not a general date parser: the first ten characters are the date and shorter text reads nil, so a datetime text and a date text do not answer through one parser. A well-width text that is not a real calendar date raises through Date.new instead of turning into nil.

Examples:

"2024-01-02".to_date   # => Date.new(2024, 1, 2)
"2024-01-0".to_date    # => nil

Returns:

  • (Date?) —

    the parsed date, or nil when the text is shorter than the stored form

Raises:

  • (Date::Error) —

    the text is ten characters or longer but not a real calendar date



24
# File 'runtime/ext/date.rb', line 24

def to_date: () -> Date?

#to_time ⇒ Time?

"2024-01-02 03:04:05.000006" → the instant, in the app's configured zone: a stored datetime column read back. Text too short to carry an instant reads nil. The parse is the stored form's fixed width, not a date-parser pipeline: the storage form is the contract, and the micros stay in Time.utc's seventh argument (a float added afterwards loses a microsecond and skews > ? bounds). The zone comes from the process TZ, which config/environment.rb pins, so the answer arrives in the app zone without a lookup.

Returns:

  • (Time?) —

    the parsed instant, or nil when the text cannot carry one



207
# File 'runtime/ext/string.rb', line 207

def to_time: () -> Time?

#underscore ⇒ String

"FooBar" → "foo_bar": camel boundaries split, - reads as _, the result downcased. The walk is spelled out rather than a regex pipeline, and no acronym or human-name table is modeled.

Returns:

  • (String) —

    the underscored, downcased form of this text



32
# File 'runtime/ext/string.rb', line 32

def underscore: () -> String