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
This commit is contained in:
nobu 2017-04-01 07:38:12 +00:00
Родитель c527fa1341
Коммит f57d515d69
3 изменённых файлов: 24 добавлений и 0 удалений

Просмотреть файл

@ -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 @@ class TestArray < Test::Unit::TestCase
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 @@ class TestArray < Test::Unit::TestCase
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))