From 078cc10d31ee39fa8960e6f491dbea3b368cbec7 Mon Sep 17 00:00:00 2001 From: Rashmi Yadav Date: Sat, 23 Aug 2014 21:44:03 +0200 Subject: [ci skip ]Document transform_values && transform_values! --- guides/source/active_support_core_extensions.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'guides/source/active_support_core_extensions.md') diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 5ed392d43d..08b46c62eb 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -2862,6 +2862,20 @@ Active Record does not accept unknown options when building associations, for ex NOTE: Defined in `active_support/core_ext/hash/keys.rb`. +### Transform 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 + +```ruby +{nil => nil, 1 => 1, a: :a}.transform_values { |value| value.to_s.upcase } +# => {nil => "", 1 => "1", :a=> "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` + ### Slicing Ruby has built-in support for taking slices out of strings and arrays. Active Support extends slicing to hashes: -- cgit v1.2.3 From 10db166aef3ac99183b8360512f62a1a2685ae40 Mon Sep 17 00:00:00 2001 From: Rashmi Yadav Date: Sun, 24 Aug 2014 01:31:37 +0200 Subject: [ci skip] document truncate_words --- guides/source/active_support_core_extensions.md | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'guides/source/active_support_core_extensions.md') diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 08b46c62eb..0bdbf1d696 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -1310,6 +1310,39 @@ In above examples "dear" gets cut first, but then `:separator` prevents it. 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`: + +```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!..." +``` + +The option `:separator` can be a regexp: + +```ruby +"Oh dear! Oh dear! I shall be late!".truncate_words(4, separator: /\s/) +# => "Oh dear! Oh dear!..." +``` + +NOTE: Defined in `active_support/core_ext/string/filters.rb`. + ### `inquiry` The `inquiry` method converts a string into a `StringInquirer` object making equality checks prettier. -- cgit v1.2.3 From 077d28fb6652a94db190aa0239ceaee35404e0ff Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Wed, 3 Sep 2014 16:12:45 +0000 Subject: copy edit AS core ext changes [ci skip] --- guides/source/active_support_core_extensions.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'guides/source/active_support_core_extensions.md') diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 0bdbf1d696..965de0c761 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -1312,12 +1312,13 @@ 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 @@ -1325,13 +1326,11 @@ Ellipsis can be customized with the `:omission` option: # => "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: @@ -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 -- cgit v1.2.3