зеркало из https://github.com/github/ruby.git
Move Array#select to Ruby
Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
This commit is contained in:
Родитель
d57d85548f
Коммит
c9a5a71695
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;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
ary_sized_alloc(rb_execution_context_t *ec, VALUE self)
|
||||
{
|
||||
return rb_ary_new2(RARRAY_LEN(self));
|
||||
}
|
||||
|
||||
static VALUE
|
||||
ary_sample0(rb_execution_context_t *ec, VALUE ary)
|
||||
{
|
||||
|
|
25
array.rb
25
array.rb
|
@ -232,5 +232,30 @@ class Array
|
|||
self
|
||||
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
|
||||
|
|
Загрузка…
Ссылка в новой задаче