Commit b91a5d53
b91a5d5315c2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
codegen: Enumerable#one?(x) arg form on int_array
The block form (`arr.one? { |x| ... }`) was already handled. `arr.one?(2)` (predicate-by-equality) fell through to the unresolved-call path. Add an arg-form arm that walks the array counting matches and returns true iff exactly one matched.
Fixes #968
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- b91a5d5315c2
- Parent
- f5973caf6713
spinel_codegen.rbmodifiedView file
@@ -22069,6 +22069,11 @@
2206922069
if mname == "empty?"2207022070
return "sp_IntArray_empty(" + rc + ")"2207122071
end22072+
if mname == "one?" && @nd_arguments[nid] >= 0 && get_args(@nd_arguments[nid]).length > 022073+
# `arr.one?(x)` — true iff exactly one element equals x. The22074+
# block form is handled by compile_array_predicate_block.22075+
return "({ mrb_int _v = " + compile_arg0_as_int(nid) + ", _c = 0; sp_IntArray *_a = " + rc + "; for (mrb_int _i = 0; _i < _a->len; _i++) if (sp_IntArray_get(_a, _i) == _v) _c++; _c == 1 ? TRUE : FALSE; })"22076+
end2207222077
if mname == "include?" || mname == "member?"2207322078
# Issue #911: cross-type include? returns false (CRuby). Sym_array2207422079
# shares the int_array dispatch (sym ids stored as ints), so allowtest/int_array_one_arg.rbaddedView file
Loading diff…
test/int_array_one_arg.rb.expectedaddedView file
Loading diff…