ruby-changes:68488
From: Jeremy <ko1@a...>
Date: Sat, 16 Oct 2021 06:00:10 +0900 (JST)
Subject: [ruby-changes:68488] 2a5c3a4d0f (master): Update documentation for String and Symbol to discuss differences
https://git.ruby-lang.org/ruby.git/commit/?id=2a5c3a4d0f From 2a5c3a4d0f693ad0fe7b76dd99155e57149d2cac Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Fri, 15 Oct 2021 13:54:03 -0700 Subject: Update documentation for String and Symbol to discuss differences Implements [Feature #14347] --- string.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/string.c b/string.c index 13079251d4..38ae3242e7 100644 --- a/string.c +++ b/string.c @@ -10958,7 +10958,7 @@ rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L10958 /********************************************************************** * Document-class: Symbol * - * Symbol objects represent names inside the Ruby interpreter. They + * Symbol objects represent named identifiers inside the Ruby interpreter. They * are generated using the <code>:name</code> and * <code>:"string"</code> literals syntax, and by the various * <code>to_sym</code> methods. The same Symbol object will be @@ -10984,6 +10984,34 @@ rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L10984 * $f2.object_id #=> 2514190 * $f3.object_id #=> 2514190 * + * Constant, method, and variable names are returned as symbols: + * + * module One + * Two = 2 + * def three; 3 end + * @four = 4 + * @@five = 5 + * $six = 6 + * end + * seven = 7 + * + * One.constants + * # => [:Two] + * One.instance_methods(true) + * # => [:three] + * One.instance_variables + * # => [:@four] + * One.class_variables + * # => [:@@five] + * global_variables.grep(/six/) + * # => [:$six] + * local_variables + * # => [:seven] + * + * Symbol objects are different from String objects in that + * Symbol objects represent identifiers, while String objects + * represent text or data. + * */ @@ -11567,8 +11595,11 @@ rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) https://github.com/ruby/ruby/blob/trunk/string.c#L11595 /* * A String object holds and manipulates an arbitrary sequence of - * bytes, typically representing characters. String objects may be created - * using String::new or as literals. + * bytes, typically representing text or binary data. String objects may be + * created using String::new or as literals. + * + * String objects differ from Symbol objects in that Symbol objects are + * designed to be used as identifiers, instead of text or data. * * Because of aliasing issues, users of strings should be aware of the methods * that modify the contents of a String object. Typically, -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/