Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias Array's Push and Unshift with Append and Prepend #1574

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions array.c
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions doc/contributors.rdoc
Expand Up @@ -600,6 +600,8 @@ Richard M. Stallman
Robin Stocker
* documentation

Joshua Stowers

Adam Strzelecki
* a patch for compile.c

Expand Down
19 changes: 19 additions & 0 deletions test/ruby/test_array.rb
Expand Up @@ -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 )]
Expand Down Expand Up @@ -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))
Expand Down