Move Array#select to Ruby
Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
This commit is contained in:
parent
d57d85548f
commit
c9a5a71695
Notes:
git
2024-11-13 20:27:44 +00:00
6
array.c
6
array.c
@ -6866,6 +6866,12 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
ary_sized_alloc(rb_execution_context_t *ec, VALUE self)
|
||||||
|
{
|
||||||
|
return rb_ary_new2(RARRAY_LEN(self));
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
ary_sample0(rb_execution_context_t *ec, VALUE ary)
|
ary_sample0(rb_execution_context_t *ec, VALUE ary)
|
||||||
{
|
{
|
||||||
|
25
array.rb
25
array.rb
@ -232,5 +232,30 @@ class Array
|
|||||||
self
|
self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if Primitive.rb_builtin_basic_definition_p(:select)
|
||||||
|
undef :select
|
||||||
|
|
||||||
|
def select # :nodoc:
|
||||||
|
Primitive.attr! :inline_block, :c_trace
|
||||||
|
|
||||||
|
unless defined?(yield)
|
||||||
|
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
|
||||||
|
end
|
||||||
|
|
||||||
|
_i = 0
|
||||||
|
value = nil
|
||||||
|
result = Primitive.ary_sized_alloc
|
||||||
|
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
|
||||||
|
result << value if yield value
|
||||||
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
if Primitive.rb_builtin_basic_definition_p(:filter)
|
||||||
|
undef :filter
|
||||||
|
alias filter select
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user