You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/active_support_core_extensions.md
+39-2Lines changed: 39 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1151,6 +1151,24 @@ In above examples "dear" gets cut first, but then `:separator` prevents it.
1151
1151
1152
1152
NOTE: Defined in `active_support/core_ext/string/filters.rb`.
1153
1153
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
+
1154
1172
### `truncate_words`
1155
1173
1156
1174
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
2038
2056
is either a passed default or returned in a block.
2039
2057
2040
2058
```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?" }
2043
2063
2044
2064
WEEKDAYS.index_with(Interval.all_day)
2045
2065
# => { monday: [ 0, 1440 ], … }
@@ -2712,6 +2732,23 @@ Active Record does not accept unknown options when building associations, for ex
2712
2732
2713
2733
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
2714
2734
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.
0 commit comments