From 5ce0d2aa354eb996cb3ca9bb944f880ff6acfd57 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Mon, 25 Apr 2022 15:59:09 -0500 Subject: [DOC] Enhanced RDoc for Kernel (#5846) Treats: #Array #Hash #String --- object.c | 63 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 26 deletions(-) (limited to 'object.c') diff --git a/object.c b/object.c index 3c0e41dcee..eaf30e0dff 100644 --- a/object.c +++ b/object.c @@ -3651,15 +3651,18 @@ rb_String(VALUE val) /* * call-seq: - * String(arg) -> string + * String(object) -> object or new_string * - * Returns arg as a String. + * Returns an array converted from +object+. * - * First tries to call its to_str method, then its to_s method. + * Tries to convert +object+ to a string + * using +to_str+ first and +to_s+ second: * - * String(self) #=> "main" - * String(self.class) #=> "Object" - * String(123456) #=> "123456" + * String([0, 1, 2]) # => "[0, 1, 2]" + * String(0..5) # => "0..5" + * String({foo: 0, bar: 1}) # => "{:foo=>0, :bar=>1}" + * + * Raises +TypeError+ if +object+ cannot be converted to a string. */ static VALUE @@ -3684,22 +3687,22 @@ rb_Array(VALUE val) /* * call-seq: - * Array(arg) -> array + * Array(object) -> object or new_array + * + * Returns an array converted from +object+. + * + * Tries to convert +object+ to an array + * using +to_ary+ first and +to_a+ second: * - * Returns +arg+ as an Array. + * Array([0, 1, 2]) # => [0, 1, 2] + * Array({foo: 0, bar: 1}) # => [[:foo, 0], [:bar, 1]] + * Array(0..4) # => [0, 1, 2, 3, 4] * - * First tries to call to_ary on +arg+, then to_a. - * If +arg+ does not respond to to_ary or to_a, - * returns an Array of length 1 containing +arg+. + * Returns +object+ in an array, [object], + * if +object+ cannot be converted: * - * If to_ary or to_a returns something other than - * an Array, raises a TypeError. + * Array(:foo) # => [:foo] * - * Array(["a", "b"]) #=> ["a", "b"] - * Array(1..5) #=> [1, 2, 3, 4, 5] - * Array(key: :value) #=> [[:key, :value]] - * Array(nil) #=> [] - * Array(1) #=> [1] */ static VALUE @@ -3728,16 +3731,24 @@ rb_Hash(VALUE val) /* * call-seq: - * Hash(arg) -> hash + * Hash(object) -> object or new_hash + * + * Returns a hash converted from +object+. + * + * - If +object+ is: + * + * - A hash, returns +object+. + * - An empty array or +nil+, returns an empty hash. + * + * - Otherwise, if object.to_hash returns a hash, returns that hash. + * - Otherwise, returns TypeError. + * + * Examples: * - * Converts arg to a Hash by calling - * arg.to_hash. Returns an empty Hash when - * arg is nil or []. + * Hash({foo: 0, bar: 1}) # => {:foo=>0, :bar=>1} + * Hash(nil) # => {} + * Hash([]) # => {} * - * Hash([]) #=> {} - * Hash(nil) #=> {} - * Hash(key: :value) #=> {:key => :value} - * Hash([1, 2, 3]) #=> TypeError */ static VALUE -- cgit v1.2.1