diff options
author | Burdette Lamar <[email protected]> | 2025-05-08 09:31:47 -0500 |
---|---|---|
committer | GitHub <[email protected]> | 2025-05-08 10:31:47 -0400 |
commit | 46a82408846fe1ec5ad6f8fbb883c886abae079a (patch) | |
tree | 04501b613961439b8ac2d961a68c3c45773c0cd6 /string.c | |
parent | 5e534849940b34ea6590da053830cf1ef53ab475 (diff) |
[DOC] Tweaks for String#-@
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/13254
Merged-By: peterzhu2118 <[email protected]>
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 35 |
1 files changed, 24 insertions, 11 deletions
@@ -3615,24 +3615,37 @@ str_uplus(VALUE str) /* * call-seq: - * -string -> frozen_string - * dedup -> frozen_string + * -self -> frozen_string * - * Returns a frozen, possibly pre-existing copy of the string. + * Returns a frozen string equal to +self+. * - * The returned +String+ will be deduplicated as long as it does not have - * any instance variables set on it and is not a String subclass. + * The returned string is +self+ if and only if all of the following are true: * - * Note that <tt>-string</tt> variant is more convenient for defining - * constants: + * - +self+ is already frozen. + * - +self+ is an instance of \String (rather than of a subclass of \String) + * - +self+ has no instance variables set on it. * - * FILENAME = -'config/database.yml' + * Otherwise, the returned string is a frozen copy of +self+. * - * while +dedup+ is better suitable for using the method in chains - * of calculations: + * Returning +self+, when possible, saves duplicating +self+; + * see {Data deduplication}[https://en.wikipedia.org/wiki/Data_deduplication]. * - * @url_list.concat(urls.map(&:dedup)) + * It may also save duplicating other, already-existing, strings: * + * s0 = 'foo' + * s1 = 'foo' + * s0.object_id == s1.object_id # => false + * (-s0).object_id == (-s1).object_id # => true + * + * Note that method #-@ is convenient for defining a constant: + * + * FileName = -'config/database.yml' + * + * While its alias #dedup is better suited for chaining: + * + * 'foo'.dedup.gsub!('o') + * + * Related: see {Methods for a Frozen/Unfrozen String}[rdoc-ref:String@Methods+for+a+Frozen-2FUnfrozen+String]. */ static VALUE str_uminus(VALUE str) |