Skip to content

Commit 91ff744

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Harmonize join methods
1 parent 3d43367 commit 91ff744

4 files changed

Lines changed: 41 additions & 34 deletions

File tree

array.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,28 +3107,20 @@ rb_ary_join(VALUE ary, VALUE sep)
31073107
* call-seq:
31083108
* join(separator = $,) -> new_string
31093109
*
3110-
* Returns the new string formed by joining the converted elements of +self+;
3111-
* for each element +element+:
3110+
* Returns the new string formed by joining the string-converted elements of +self+
3111+
* with the given +separator+ (defaults to <tt>$,</tt>):
31123112
*
3113-
* - Converts recursively using <tt>element.join(separator)</tt>
3114-
* if +element+ is a <tt>kind_of?(Array)</tt>.
3115-
* - Otherwise, converts using <tt>element.to_s</tt>.
3113+
* $, # => nil
3114+
* %w[].join # => ""
3115+
* %w[foo].join # => "foo"
3116+
* a = %w[foo bar baz] # => ["foo", "bar", "baz"]
3117+
* a.join # => "foobarbaz"
3118+
* a.join('|') # => "foo|bar|baz"
3119+
* a.join(' :|: ') # => "foo :|: bar :|: baz"
31163120
*
3117-
* With no argument given, joins using the output field separator, <tt>$,</tt>:
3121+
* Flattens and joins nested arrays:
31183122
*
3119-
* a = [:foo, 'bar', 2]
3120-
* $, # => nil
3121-
* a.join # => "foobar2"
3122-
*
3123-
* With string argument +separator+ given, joins using that separator:
3124-
*
3125-
* a = [:foo, 'bar', 2]
3126-
* a.join("\n") # => "foo\nbar\n2"
3127-
*
3128-
* Joins recursively for nested arrays:
3129-
*
3130-
* a = [:foo, [:bar, [:baz, :bat]]]
3131-
* a.join # => "foobarbazbat"
3123+
* [:foo, [:bar, [:baz, :bat]]].join # => "foobarbazbat"
31323124
*
31333125
* Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
31343126
*/

file.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5615,12 +5615,14 @@ rb_file_join(long argc, VALUE *args)
56155615
}
56165616
/*
56175617
* call-seq:
5618-
* File.join(string, ...) -> string
5618+
* File.join(*objects) -> new_string
56195619
*
5620-
* Returns a new string formed by joining the strings using
5621-
* <code>"/"</code>.
5620+
* Returns a new string formed by joining the given string-converted +objects+
5621+
* with character <tt>'/'</tt>:
56225622
*
5623-
* File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"
5623+
* File.join # => ""
5624+
* File.join('foo') # => "foo"
5625+
* File.join('foo', 'bar', 'baz') # => "foo/bar/baz"
56245626
*
56255627
*/
56265628

pathname_builtin.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -759,17 +759,16 @@ def plus(path1, path2) # :nodoc:
759759
end
760760
private :plus
761761

762+
# call-seq:
763+
# join(*objects) -> new_pathname
762764
#
763-
# Joins the given pathnames onto +self+ to create a new Pathname object.
764-
# This is effectively the same as using Pathname#+ to append +self+ and
765-
# all arguments sequentially.
765+
# Joins the string-converted given +objects+ to the string path in +self+;
766+
# returns a new pathname containing the joined string:
766767
#
767-
# path0 = Pathname.new("/usr") # Pathname:/usr
768-
# path0 = path0.join("bin/ruby") # Pathname:/usr/bin/ruby
769-
# # is the same as
770-
# path1 = Pathname.new("/usr") + "bin/ruby" # Pathname:/usr/bin/ruby
771-
# path0 == path1
772-
# #=> true
768+
# Pathname('foo').join # => #<Pathname:foo>
769+
# Pathname('foo').join('bar') # => #<Pathname:foo/bar>
770+
# Pathname('foo').join('bar', 'baz') # => #<Pathname:foo/bar/baz>
771+
# Pathname('foo').join(Pathname('bar')) # => #<Pathname:foo/bar>
773772
#
774773
def join(*args)
775774
return self if args.empty?

set.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,23 @@ set_i_to_set(VALUE set)
686686

687687
/*
688688
* call-seq:
689-
* join(separator=nil)-> new_string
689+
* join(separator = $,) -> new_string
690+
*
691+
* Returns the new string formed by joining the string-converted elements of +self+
692+
* with the given +separator+ (defaults to <tt>$,</tt>):
693+
*
694+
* $, # => nil
695+
* Set[].join # => ""
696+
* Set[%w[foo]].join # => "foo"
697+
* s = Set[%w[foo bar baz]] # => Set[["foo", "bar", "baz"]]
698+
* s.join # => "foobarbaz"
699+
* s.join('|') # => "foo|bar|baz"
700+
* s.join(' :|: ') # => "foo :|: bar :|: baz"
701+
*
702+
* Flattens and joins nested arrays:
703+
*
704+
* Set[[:foo, [:bar, [:baz, :bat]]]].join0 # => "foobarbazbat"
690705
*
691-
* Returns a string created by converting each element of the set to a string.
692706
*/
693707
static VALUE
694708
set_i_join(int argc, VALUE *argv, VALUE set)

0 commit comments

Comments
 (0)