coreext

package
v0.0.0-...-f0d72c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package coreext is a pure-Go (no cgo), MRI-faithful reimplementation of the highest-value ActiveSupport core-extension helpers (String, Array, Hash, Object/Numeric, Enumerable). Each helper is a plain Go function that the rbgo binding maps onto the corresponding Ruby monkey-patch.

Representation choices (documented so the rbgo binding can map cleanly):

  • Ruby String → Go string (helpers are rune-aware where Ruby is character-aware).
  • Ruby Array → Go []any (nil padding is represented by Go nil).
  • Ruby Hash → Go map[any]any so string and Symbol keys can coexist.
  • Ruby Symbol → the Symbol type below.
  • Behaviour that needs Ruby object semantics (Object#try, blank? on an arbitrary object) is reached through an explicit seam (Dispatcher / Blankable) the binding supplies.

String inflection helpers (Pluralize, Camelize, …) delegate to the sibling inflector package, so they inherit its byte-for-byte MRI fidelity.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayBlank

func ArrayBlank(a []any) bool

ArrayBlank reports whether the array is empty (Array#blank?).

func ArrayFrom

func ArrayFrom(a []any, pos int) ([]any, bool)

ArrayFrom returns the elements from index pos to the end (negative counts from the end), reporting false when pos is past the end (Array#from).

func ArrayTo

func ArrayTo(a []any, pos int) []any

ArrayTo returns the elements from the start through index pos inclusive (negative counts from the end) (Array#to).

func AssertValidKeys

func AssertValidKeys(h map[any]any, valid ...any) error

AssertValidKeys returns an error if h contains a key not in valid, mirroring Hash#assert_valid_keys (which raises ArgumentError).

func At

func At(s string, pos int) (string, bool)

At returns the character at position pos (negative counts from the end), reporting false when pos is out of range (String#at / String#[]).

func Blank

func Blank(v any) bool

Blank reports whether v is "blank" in the ActiveSupport sense: nil, false, an empty/whitespace string, an empty array or hash, or a Blankable that says so. Everything else (including any number) is present (Object#blank?).

func Camelize

func Camelize(s string) string

Camelize converts s to UpperCamelCase (String#camelize).

func CamelizeLower

func CamelizeLower(s string) string

CamelizeLower converts s to lowerCamelCase (String#camelize(:lower)).

func Classify

func Classify(s string) string

Classify turns a table name into a class name (String#classify).

func Dasherize

func Dasherize(s string) string

Dasherize replaces underscores with dashes (String#dasherize).

func DeepDup

func DeepDup(h map[any]any) map[any]any

DeepDup returns a recursive copy of h, duplicating nested hashes and slices (Hash#deep_dup).

func DeepMerge

func DeepMerge(h, other map[any]any) map[any]any

DeepMerge returns a new hash merging other into h recursively: when both values for a key are hashes they are deep-merged, otherwise other wins (Hash#deep_merge).

func DeepMergeInto

func DeepMergeInto(h, other map[any]any) map[any]any

DeepMergeInto merges other into h in place and returns h (Hash#deep_merge!).

func DeepStringifyKeys

func DeepStringifyKeys(h map[any]any) map[any]any

DeepStringifyKeys is StringifyKeys applied recursively to nested hashes (Hash#deep_stringify_keys).

func DeepSymbolizeKeys

func DeepSymbolizeKeys(h map[any]any) map[any]any

DeepSymbolizeKeys is SymbolizeKeys applied recursively to nested hashes (Hash#deep_symbolize_keys).

func DeepTransformValues

func DeepTransformValues(h map[any]any, fn func(any) any) map[any]any

DeepTransformValues returns a copy of h with fn applied to every value, recursing into nested hashes and slices (Hash#deep_transform_values).

func EndsWith

func EndsWith(s, suffix string) bool

EndsWith reports whether s ends with suffix (String#ends_with?).

func Except

func Except(h map[any]any, keys ...any) map[any]any

Except returns a copy of h without the given keys (Hash#except).

func Exclude

func Exclude(items []any, v any) bool

Exclude reports whether v is not present in items (Enumerable#exclude?).

func ExtractOptions

func ExtractOptions(a []any) ([]any, map[any]any)

ExtractOptions removes and returns a trailing options hash, mirroring Array#extract_options!. It returns the remaining elements and the options (an empty map when the last element is not a map[any]any).

func Fifth

func Fifth(a []any) any

Fifth returns the fifth element or nil (Array#fifth).

func First

func First(s string, n int) string

First returns the first n characters of s (String#first). n defaults to 1 via FirstChar; values past the length return the whole string, 0 returns "".

func FirstChar

func FirstChar(s string) string

FirstChar returns the first character of s (String#first with no argument).

func Fourth

func Fourth(a []any) any

Fourth returns the fourth element or nil (Array#fourth).

func From

func From(s string, pos int) (string, bool)

From returns the substring from position pos to the end (negative counts from the end), reporting false when pos is past the end (String#from).

func HashBlank

func HashBlank(h map[any]any) bool

HashBlank reports whether the hash is empty (Hash#blank?).

func Humanize

func Humanize(s string) string

Humanize humanizes s (String#humanize).

func InGroups

func InGroups(a []any, number int, fill any) [][]any

InGroups splits a into number groups, padding shorter groups with fill (Array#in_groups). Use InGroupsNoFill to leave short groups unpadded.

func InGroupsNoFill

func InGroupsNoFill(a []any, number int) [][]any

InGroupsNoFill splits a into number groups without padding (Array#in_groups(n, false)).

func InGroupsOf

func InGroupsOf(a []any, number int, fill any) [][]any

InGroupsOf splits a into consecutive groups of number elements, padding the final group with fill (Array#in_groups_of).

func InGroupsOfNoFill

func InGroupsOfNoFill(a []any, number int) [][]any

InGroupsOfNoFill splits a into groups of number elements without padding the final group (Array#in_groups_of(n, false)).

func Indent

func Indent(s string, amount int, indentChar string, indentEmptyLines bool) string

Indent prefixes each non-skipped line of s with amount copies of indentChar (default " "). Empty lines are indented only when indentEmptyLines is true (String#indent).

func IndexBy

func IndexBy(items []any, key func(any) any) map[any]any

IndexBy builds a map from key(elem) to the last element with that key (Enumerable#index_by).

func Last

func Last(s string, n int) string

Last returns the last n characters of s (String#last).

func LastChar

func LastChar(s string) string

LastChar returns the last character of s (String#last with no argument).

func Many

func Many(items []any) bool

Many reports whether items has more than one element (Enumerable#many?).

func ManyBy

func ManyBy(items []any, pred func(any) bool) bool

ManyBy reports whether more than one element satisfies pred (Enumerable#many? with a block).

func MultipleOf

func MultipleOf(n, m int) bool

MultipleOf reports whether n is an integer multiple of m (Integer#multiple_of?). When m is zero, only zero is a multiple.

func Ordinal

func Ordinal(n int) string

Ordinal returns the ordinal suffix for n (Integer#ordinal).

func Ordinalize

func Ordinalize(n int) string

Ordinalize renders n with its ordinal suffix (Integer#ordinalize).

func Parameterize

func Parameterize(s string) string

Parameterize slugifies s with a "-" separator (String#parameterize).

func Pick

func Pick(items []any, key func(any) any) (any, bool)

Pick returns key(elem) for the first element, reporting false when items is empty (Enumerable#pick).

func Pluck

func Pluck(items []any, key func(any) any) []any

Pluck returns key(elem) for every element (Enumerable#pluck).

func Pluralize

func Pluralize(s string) string

Pluralize returns the plural of s (String#pluralize).

func Presence

func Presence(v any) any

Presence returns v when it is present, or nil when it is blank (Object#presence).

func Present

func Present(v any) bool

Present is the inverse of Blank (Object#present?).

func Remove

func Remove(s string, patterns ...string) string

Remove deletes every occurrence of each pattern from s (String#remove).

func ReverseMerge

func ReverseMerge(h, other map[any]any) map[any]any

ReverseMerge returns other merged with h, where h's values win on conflict (Hash#reverse_merge).

func Second

func Second(a []any) any

Second returns the second element or nil (Array#second).

func Singularize

func Singularize(s string) string

Singularize returns the singular of s (String#singularize).

func Slice

func Slice(h map[any]any, keys ...any) map[any]any

Slice returns a copy of h containing only the given keys that are present (Hash#slice).

func Split

func Split(a []any, sep any) [][]any

Split divides a into subarrays separated by every element equal to sep (Array#split with a value).

func Squish

func Squish(s string) string

Squish strips leading/trailing whitespace and collapses internal whitespace runs to a single space (String#squish).

func StartsWith

func StartsWith(s, prefix string) bool

StartsWith reports whether s begins with prefix (String#starts_with?).

func StringBlank

func StringBlank(s string) bool

StringBlank reports whether s is empty or contains only whitespace (ActiveSupport's String#blank?, matching /\A[[:space:]]*\z/).

func StringPresence

func StringPresence(s string) (string, bool)

StringPresence returns (s, true) when s is present, or ("", false) when it is blank (String#presence returns the string or nil).

func StringPresent

func StringPresent(s string) bool

StringPresent is the inverse of StringBlank (String#present?).

func StringifyKeys

func StringifyKeys(h map[any]any) map[any]any

StringifyKeys returns a copy of h with every key converted to its string form (Hash#stringify_keys).

func StripHeredoc

func StripHeredoc(s string) string

StripHeredoc removes the smallest common leading indentation from every line (String#strip_heredoc).

func Sum

func Sum(items []any, init float64, project func(any) float64) float64

Sum adds init to the sum of project(elem) over items (Enumerable#sum). Pass a nil project to sum the elements themselves when they are float64.

func SymbolizeKeys

func SymbolizeKeys(h map[any]any) map[any]any

SymbolizeKeys returns a copy of h with string keys converted to Symbol (Hash#symbolize_keys). Non-string keys are left unchanged.

func Tableize

func Tableize(s string) string

Tableize turns a class name into a table name (String#tableize).

func Third

func Third(a []any) any

Third returns the third element or nil (Array#third).

func Titleize

func Titleize(s string) string

Titleize titleizes s (String#titleize).

func To

func To(s string, pos int) string

To returns the substring from the start through position pos inclusive (negative counts from the end) (String#to).

func ToSentence

func ToSentence(items []any) string

ToSentence joins items into a human sentence using the default English connectors ("a, b, and c") (Array#to_sentence).

func ToSentenceWith

func ToSentenceWith(items []any, wordsConnector, twoWordsConnector, lastWordConnector string) string

ToSentenceWith joins items using explicit connectors (Array#to_sentence with :words_connector / :two_words_connector / :last_word_connector).

func Truncate

func Truncate(s string, length int, omission, separator string) string

Truncate shortens s to at most length characters, appending omission (default "..." when empty). When separator is non-empty, the cut is moved back to the last separator boundary (String#truncate).

func TruncateWords

func TruncateWords(s string, wordsCount int, omission string) string

TruncateWords keeps the first wordsCount whitespace-separated words, appending omission (default "...") when the text is longer (String#truncate_words).

func Try

func Try(recv any, dispatch Dispatcher, method string, args ...any) any

Try invokes method on recv through dispatch, returning nil when recv is nil or does not respond to method (Object#try). This is the safe-navigation helper; dispatch is the Ruby-semantics seam.

func Underscore

func Underscore(s string) string

Underscore converts s to snake_case (String#underscore).

Types

type Blankable

type Blankable interface{ IsBlank() bool }

Blankable is the seam for objects that define their own blank? via an empty?-like predicate. The rbgo binding implements it for Ruby objects that respond to empty?.

type Dispatcher

type Dispatcher func(recv any, method string, args []any) (result any, responded bool)

Dispatcher is the method-dispatch seam used by Try: it invokes method on recv and reports whether recv responds to it. The rbgo binding backs this with the Ruby object model.

type Symbol

type Symbol string

Symbol represents a Ruby Symbol (e.g. :name). It is distinct from string so symbolize/stringify round-trips are observable, exactly as in Ruby.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL