Commit 9fe24709
9fe24709c4aeMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#one? with block
Returns true when exactly one element produces a truthy block result. Short-circuits on the second truthy result.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 9fe24709c4ae
- Parent
- a269817036d1
spinel_codegen.rbmodifiedView file
@@ -1628,7 +1628,7 @@
16281628
if mname == "empty?"16291629
return "bool"16301630
end1631-
if mname == "any?" || mname == "all?" || mname == "none?"1631+
if mname == "any?" || mname == "all?" || mname == "none?" || mname == "one?"16321632
return "bool"16331633
end16341634
if mname == "between?"@@ -12348,7 +12348,7 @@
1234812348
pfx = array_c_prefix(recv_type)1234912349
return "(sp_" + pfx + "_length(" + rc + ") == 0)"1235012350
end12351-
if (mname == "any?" || mname == "all?" || mname == "none?") && @nd_block[nid] >= 012351+
if (mname == "any?" || mname == "all?" || mname == "none?" || mname == "one?") && @nd_block[nid] >= 01235212352
return compile_array_predicate_block(nid, rc, recv_type, mname)1235312353
end1235412354
if (mname == "find" || mname == "detect") && @nd_block[nid] >= 0@@ -17124,6 +17124,8 @@
1712417124
emit(" if (" + bexpr + ") { " + tmp_res + " = TRUE; break; }")1712517125
elsif mname == "all?"1712617126
emit(" if (!(" + bexpr + ")) { " + tmp_res + " = FALSE; break; }")17127+
elsif mname == "one?"17128+
emit(" if (" + bexpr + ") { if (" + tmp_res + ") { " + tmp_res + " = FALSE; break; } " + tmp_res + " = TRUE; }")1712717129
else1712817130
# none?1712917131
emit(" if (" + bexpr + ") { " + tmp_res + " = FALSE; break; }")