Skip to content

Commit 2b935b2

Browse files
committed
Implement rb_array_sort and rb_array_sort_bang
1 parent 2a319c2 commit 2b935b2

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Compatibility:
4646
* Implemented `rb_uv_to_utf8` (#1998).
4747
* Single character IDs now behave more like those in MRI to improve C extension compatibility, so `rb_funcall(a, '+', b)` will now do the same thing as in MRI.
4848
* Removed extra public methods on `String`.
49+
* Implemented `rb_array_sort` and `rb_array_sort_bang`.
4950

5051
Performance:
5152

spec/ruby/optional/capi/array_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@
190190
end
191191
end
192192

193+
describe "rb_ary_sort" do
194+
it "returns a new sorted array" do
195+
a = [2, 1, 3]
196+
@s.rb_ary_sort(a).should == [1, 2, 3]
197+
a.should == [2, 1, 3]
198+
end
199+
end
200+
201+
describe "rb_ary_sort_bang" do
202+
it "sorts the given array" do
203+
a = [2, 1, 3]
204+
@s.rb_ary_sort_bang(a).should == [1, 2, 3]
205+
a.should == [1, 2, 3]
206+
end
207+
end
208+
193209
describe "rb_ary_store" do
194210
it "overwrites the element at the given position" do
195211
a = [1, 2, 3]

spec/ruby/optional/capi/ext/array_spec.c

+10
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ static VALUE array_spec_rb_ary_shift(VALUE self, VALUE array) {
162162
return rb_ary_shift(array);
163163
}
164164

165+
static VALUE array_spec_rb_ary_sort(VALUE self, VALUE array) {
166+
return rb_ary_sort(array);
167+
}
168+
169+
static VALUE array_spec_rb_ary_sort_bang(VALUE self, VALUE array) {
170+
return rb_ary_sort_bang(array);
171+
}
172+
165173
static VALUE array_spec_rb_ary_store(VALUE self, VALUE array, VALUE offset, VALUE value) {
166174
rb_ary_store(array, FIX2INT(offset), value);
167175

@@ -272,6 +280,8 @@ void Init_array_spec(void) {
272280
rb_define_method(cls, "rb_ary_reverse", array_spec_rb_ary_reverse, 1);
273281
rb_define_method(cls, "rb_ary_rotate", array_spec_rb_ary_rotate, 2);
274282
rb_define_method(cls, "rb_ary_shift", array_spec_rb_ary_shift, 1);
283+
rb_define_method(cls, "rb_ary_sort", array_spec_rb_ary_sort, 1);
284+
rb_define_method(cls, "rb_ary_sort_bang", array_spec_rb_ary_sort_bang, 1);
275285
rb_define_method(cls, "rb_ary_store", array_spec_rb_ary_store, 3);
276286
rb_define_method(cls, "rb_ary_concat", array_spec_rb_ary_concat, 2);
277287
rb_define_method(cls, "rb_ary_plus", array_spec_rb_ary_plus, 2);

src/main/c/cext/array.c

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ VALUE rb_ary_pop(VALUE array) {
5858
return RUBY_INVOKE(array, "pop");
5959
}
6060

61+
VALUE rb_ary_sort(VALUE array) {
62+
return RUBY_INVOKE(array, "sort");
63+
}
64+
65+
VALUE rb_ary_sort_bang(VALUE array) {
66+
return RUBY_INVOKE(array, "sort!");
67+
}
68+
6169
void rb_ary_store(VALUE array, long index, VALUE value) {
6270
RUBY_INVOKE_NO_WRAP(array, "[]=", LONG2FIX(index), value);
6371
}

0 commit comments

Comments
 (0)