Commit 887c1e30
887c1e30ce20Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#cycle(n) with block
Iterates the array n times, calling the block for each element. Uses nested for loops. Works for all array types.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 887c1e30ce20
- Parent
- 9fe24709c4ae
spinel_codegen.rbmodifiedView file
@@ -15259,6 +15259,32 @@
1525915259
end1526015260
end1526115261
15262+
if mname == "cycle"15263+
if @nd_block[nid] >= 0 && recv >= 015264+
old = @in_loop15265+
@in_loop = 115266+
rt = infer_type(recv)15267+
rc = compile_expr(recv)15268+
n = compile_arg0(nid)15269+
bp1 = get_block_param(nid, 0)15270+
if bp1 == ""15271+
bp1 = "_x"15272+
end15273+
pfx = array_c_prefix(rt)15274+
tmp_c = new_temp15275+
tmp_i = new_temp15276+
emit(" for (mrb_int " + tmp_c + " = 0; " + tmp_c + " < " + n + "; " + tmp_c + "++)")15277+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")15278+
emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")15279+
@indent = @indent + 115280+
compile_stmts_body(@nd_body[@nd_block[nid]])15281+
@indent = @indent - 115282+
emit(" }")15283+
@in_loop = old15284+
return 115285+
end15286+
end15287+
1526215288
# scan with block: str.scan(/re/) { |m| ... }1526315289
if mname == "scan"1526415290
if @nd_block[nid] >= 0