Utility extensions for Ruby core classes (String, Hash, Array, File) and Rails ApplicationRecord.
- Installation
- String Extensions
- Hash Extensions
- Array Extensions
- File Extensions
- ApplicationRecord Extensions
- Contributing
- License
gem "immosquare-extensions"Converts "true" and "false" strings to boolean values. Returns nil (or a default value) for other strings.
"true".to_boolean # => true
"false".to_boolean # => false
"TRUE".to_boolean # => true (case-insensitive)
"other".to_boolean # => nil
# With default value
"other".to_boolean(true) # => true
"other".to_boolean("default") # => "default"Titleizes strings while preserving hyphens. Useful for city names and hyphenated words.
Note: Requires ActiveSupport (available in Rails applications).
"SANT-ANDREA-D'ORCINO".titleize_custom # => "Sant-Andrea-D'orcino"
"jean-pierre".titleize_custom # => "Jean-Pierre"
"hello world".titleize_custom # => "Hello World"Removes multiple keys from a hash in a single operation.
{a: 1, b: 2, c: 3}.without(:a, :b) # => {c: 3}
{a: 1, b: 2}.without(:x) # => {a: 1, b: 2} (non-existent keys ignored)Returns the nesting depth of a hash.
{a: 1}.depth # => 1
{a: {b: 1}}.depth # => 2
{a: {b: {c: 1}}}.depth # => 3
{}.depth # => 0Sorts a hash by its keys. Optionally sorts nested hashes recursively. Sorting is case-insensitive.
{b: 1, a: 2}.sort_by_key
# => {a: 2, b: 1}
{b: 1, a: {d: 4, c: 3}}.sort_by_key
# => {a: {c: 3, d: 4}, b: 1}
# Without recursion
{b: 1, a: {d: 4, c: 3}}.sort_by_key(recursive: false)
# => {a: {d: 4, c: 3}, b: 1}
# With custom sorting block
{b: 1, a: 2, c: 3}.sort_by_key { |x, y| y <=> x }
# => {c: 3, b: 1, a: 2}Flattens a nested hash into a single-level hash with dot notation keys.
{a: {b: {c: 1}}}.flatten_hash
# => {:"a.b.c" => 1}
{a: 1, b: {c: 2, d: 3}}.flatten_hash
# => {a: 1, :"b.c" => 2, :"b.d" => 3}Renders the hash as a formatted JSON string with aligned colons and customizable indentation.
Options:
| Option | Default | Description |
|---|---|---|
align |
true |
Aligns colons in key-value pairs |
indent_size |
2 |
Number of spaces per indentation level |
hash = {
name: "John",
age: 30,
address: {
street: "123 Apple St",
city: "FruitVille"
},
active: true,
scores: [85, 90, 78]
}
puts hash.to_beautiful_jsonOutput (aligned):
{
"name": "John",
"age": 30,
"address": {
"street": "123 Apple St",
"city": "FruitVille"
},
"active": true,
"scores": [
85,
90,
78
]
}Without alignment:
puts hash.to_beautiful_json(align: false){
"name": "John",
"age": 30,
"address": {
"street": "123 Apple St",
"city": "FruitVille"
},
"active": true,
"scores": [
85,
90,
78
]
}Calculates the arithmetic mean (average) of numerical arrays.
[1, 2, 3, 4, 5].mean # => 3.0
[2, 4, 6].mean # => 4.0
[10].mean # => 10.0
[].mean # => NaN (division by zero)Renders the array as a formatted JSON string, with the same options as Hash#to_beautiful_json.
data = [
{name: "Alice", age: 25},
{name: "Bob", age: 30}
]
puts data.to_beautiful_jsonOutput:
[
{
"name": "Alice",
"age": 25
},
{
"name": "Bob",
"age": 30
}
]Ensures a file ends with exactly one newline character. Removes trailing empty lines and adds a newline if missing. Returns the total number of lines.
# File content: "line1\nline2\nline3" (no trailing newline)
File.normalize_last_line("path/to/file.txt")
# File content becomes: "line1\nline2\nline3\n"
# => 3
# File content: "line1\nline2\n\n\n" (multiple trailing newlines)
File.normalize_last_line("path/to/file.txt")
# File content becomes: "line1\nline2\n"
# => 2These extensions are automatically included in ActiveRecord::Base when using Rails.
Accesses nested attributes on ActiveRecord models without manual nil checks. Returns nil if any intermediate value is missing.
user = User.first
# Instead of:
user.profile&.card_type&.slug
# You can write:
user.dig(:profile, :card_type, :slug) # => "premium"
user.dig(:profile, :missing, :slug) # => nilBug reports and pull requests are welcome on GitHub at https://github.com/immosquare/immosquare-extensions.
The gem is available as open-source under the terms of the MIT License.