Permalink
Browse files

array.c: Array#append and Array#prepend

* array.c (Init_Array): Add alias "append" to Array#push, and
  "prepend" to Array#unshift.  [Feature #12746] [Fix GH-1574]

Author:    pascbjumper2 <stowers.joshua@live.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58227 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
nobu committed Apr 1, 2017
1 parent c527fa1 commit f57d515d69b7a35477b9ba5d08fe117df1f1e275
Showing with 24 additions and 0 deletions.
  1. +2 −0 array.c
  2. +3 −0 doc/contributors.rdoc
  3. +19 −0 test/ruby/test_array.rb
@@ -6166,9 +6166,11 @@ Init_Array(void)
rb_define_method(rb_cArray, "concat", rb_ary_concat_multi, -1);
rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);
rb_define_alias(rb_cArray, "append", "push");
rb_define_method(rb_cArray, "pop", rb_ary_pop_m, -1);
rb_define_method(rb_cArray, "shift", rb_ary_shift_m, -1);
rb_define_method(rb_cArray, "unshift", rb_ary_unshift_m, -1);
rb_define_alias(rb_cArray, "prepend", "unshift");
rb_define_method(rb_cArray, "insert", rb_ary_insert, -1);
rb_define_method(rb_cArray, "each", rb_ary_each, 0);
rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0);
@@ -600,6 +600,9 @@ Richard M. Stallman
Robin Stocker
* documentation

Joshua Stowers
* a patch for array.c

Adam Strzelecki
* a patch for compile.c

@@ -443,6 +443,17 @@ def test_ASET # '[]='
assert_equal([1, 2], a)
end

def test_append
a = @cls[1, 2, 3]
assert_equal(@cls[1, 2, 3, 4, 5], a.append(4, 5))
assert_equal(@cls[1, 2, 3, 4, 5, nil], a.append(nil))

a.append
assert_equal @cls[1, 2, 3, 4, 5, nil], a
a.append 6, 7
assert_equal @cls[1, 2, 3, 4, 5, nil, 6, 7], a
end

def test_assoc
a1 = @cls[*%w( cat feline )]
a2 = @cls[*%w( dog canine )]
@@ -1204,6 +1215,14 @@ def test_pop
assert_equal(@cls[], a)
end

def test_prepend
a = @cls[]
assert_equal(@cls['cat'], a.prepend('cat'))
assert_equal(@cls['dog', 'cat'], a.prepend('dog'))
assert_equal(@cls[nil, 'dog', 'cat'], a.prepend(nil))
assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.prepend(@cls[1, 2]))
end

def test_push
a = @cls[1, 2, 3]
assert_equal(@cls[1, 2, 3, 4, 5], a.push(4, 5))

0 comments on commit f57d515

Please sign in to comment.