Skip to content

Commit

Permalink
copy edit AS core ext changes [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaydev committed Sep 3, 2014
1 parent 4b9d22e commit 077d28f
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions guides/source/active_support_core_extensions.md
Expand Up @@ -1312,26 +1312,25 @@ NOTE: Defined in `active_support/core_ext/string/filters.rb`.

### `truncate_words`

The method `truncate_words` returns a copy of its receiver truncated after a given number of `words`:
The method `truncate_words` returns a copy of its receiver truncated after a given number of words:

```ruby
"Oh dear! Oh dear! I shall be late!".truncate_words(4)
# => "Oh dear! Oh dear!..."
```

Ellipsis can be customized with the `:omission` option:

```ruby
"Oh dear! Oh dear! I shall be late!".truncate_words(4, omission: '…')
# => "Oh dear! Oh dear!…"
```

Note in particular that truncation takes into account the length of the omission string.

Pass a `:separator` to truncate the string at a natural break:

```ruby
"Oh dear! Oh dear! I shall be late!".truncate_words(4, separator: ' ')
# => "Oh dear! Oh dear!..."
"Oh dear! Oh dear! I shall be late!".truncate_words(3, separator: '!')
# => "Oh dear! Oh dear! I shall be late..."
```

The option `:separator` can be a regexp:
Expand Down Expand Up @@ -2895,19 +2894,19 @@ Active Record does not accept unknown options when building associations, for ex

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

### Transform Values
### Working with Values

#### `transform_values` && `transform_values!`

The method `transform_values` accepts a block and returns a hash that has applied the block operations to each of the values in the receiver
The method `transform_values` accepts a block and returns a hash that has applied the block operations to each of the values in the receiver.

```ruby
{nil => nil, 1 => 1, a: :a}.transform_values { |value| value.to_s.upcase }
# => {nil => "", 1 => "1", :a=> "A"}
{ nil => nil, 1 => 1, :x => :a }.transform_values { |value| value.to_s.upcase }
# => {nil=>"", 1=>"1", :x=>"A"}
```
There's also the bang variant `transform_values!` that applies the block operations to values in the very receiver.

NOTE: Defined in `active_support/core_text/hash/transform_values.rb`
NOTE: Defined in `active_support/core_text/hash/transform_values.rb`.

### Slicing

Expand Down

0 comments on commit 077d28f

Please sign in to comment.