Skip to content

Commit 81c52b1

Browse files
Sean0628georgeclaghorn
authored andcommitted
Edit AS core extension docs [ci skip]
Add String#truncate_bytes and Hash#deep_transform_values/!. Clarify Enumerable#index_with.
1 parent 927da2f commit 81c52b1

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

guides/source/active_support_core_extensions.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,24 @@ In above examples "dear" gets cut first, but then `:separator` prevents it.
11511151

11521152
NOTE: Defined in `active_support/core_ext/string/filters.rb`.
11531153

1154+
### `truncate_bytes`
1155+
1156+
The method `truncate_bytes` returns a copy of its receiver truncated to at most `bytesize` bytes:
1157+
1158+
```ruby
1159+
"👍👍👍👍".truncate_bytes(15)
1160+
# => "👍👍👍…"
1161+
```
1162+
1163+
Ellipsis can be customized with the `:omission` option:
1164+
1165+
```ruby
1166+
"👍👍👍👍".truncate_bytes(15, omission: "🖖")
1167+
# => "👍👍🖖"
1168+
```
1169+
1170+
NOTE: Defined in `active_support/core_ext/string/filters.rb`.
1171+
11541172
### `truncate_words`
11551173

11561174
The method `truncate_words` returns a copy of its receiver truncated after a given number of words:
@@ -2038,8 +2056,10 @@ The method `index_with` generates a hash with the elements of an enumerable as k
20382056
is either a passed default or returned in a block.
20392057

20402058
```ruby
2041-
%i( title body created_at ).index_with { |attr_name| post.public_send(attr_name) }
2042-
# => { title: "hey", body: "what's up?", … }
2059+
post = Post.new(title: "hey there", body: "what's up?")
2060+
2061+
%i( title body ).index_with { |attr_name| post.public_send(attr_name) }
2062+
# => { title: "hey there", body: "what's up?" }
20432063

20442064
WEEKDAYS.index_with(Interval.all_day)
20452065
# => { monday: [ 0, 1440 ], … }
@@ -2712,6 +2732,23 @@ Active Record does not accept unknown options when building associations, for ex
27122732

27132733
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
27142734

2735+
### Working with Values
2736+
2737+
#### `deep_transform_values` and `deep_transform_values!`
2738+
2739+
The method `deep_transform_values` returns a new hash with all values converted by the block operation. This includes the values from the root hash and from all nested hashes and arrays.
2740+
2741+
```ruby
2742+
hash = { person: { name: 'Rob', age: '28' } }
2743+
2744+
hash.deep_transform_values{ |value| value.to_s.upcase }
2745+
# => {person: {name: "ROB", age: "28"}}
2746+
```
2747+
2748+
There's also the bang variant `deep_transform_values!` that destructively converts all values by using the block operation.
2749+
2750+
NOTE: Defined in `active_support/core_ext/hash/deep_transform_values.rb`.
2751+
27152752
### Slicing
27162753

27172754
The method `slice!` replaces the hash with only the given keys and returns a hash containing the removed key/value pairs.

0 commit comments

Comments
 (0)