Skip to content

Commit

Permalink
Merge pull request #5646 from koic/support_array_join_with_string_arg…
Browse files Browse the repository at this point in the history
…ument

Make `Array#*` the CRuby compatible behavior when giving a string argument
  • Loading branch information
matz committed Feb 11, 2022
2 parents 11e7ca5 + 182096d commit 0ed3fcf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/array.c
Expand Up @@ -425,9 +425,15 @@ mrb_ary_times(mrb_state *mrb, mrb_value self)
{
struct RArray *a1 = mrb_ary_ptr(self);
struct RArray *a2;
mrb_value *ptr;
mrb_value *ptr, sep, tmp;
mrb_int times, len1;

mrb_get_args(mrb, "o", &sep);
tmp = mrb_check_string_type(mrb, sep);
if (!mrb_nil_p(tmp)) {
return mrb_ary_join(mrb, self, tmp);
}

mrb_get_args(mrb, "i", &times);
if (times < 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument");
Expand Down
2 changes: 2 additions & 0 deletions test/t/array.rb
Expand Up @@ -32,6 +32,8 @@ class SubArray < Array
end
assert_equal([1, 1, 1], [1].*(3))
assert_equal([], [1].*(0))
assert_equal('abc', ['a', 'b', 'c'].*(''))
assert_equal('0, 0, 1, {:foo=>0}', [0, [0, 1], {foo: 0}].*(', '))
end

assert('Array#<<', '15.2.12.5.3') do
Expand Down

0 comments on commit 0ed3fcf

Please sign in to comment.