From 2a5c3a4d0f693ad0fe7b76dd99155e57149d2cac Mon Sep 17 00:00:00 2001 From: Jeremy Evans 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(-) (limited to 'string.c') 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) /********************************************************************** * 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 :name and * :"string" literals syntax, and by the various * to_sym methods. The same Symbol object will be @@ -10984,6 +10984,34 @@ rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str) * $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) /* * 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.3