Commit 1e6cd661
1e6cd661640fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Implement Array#each_index
arr.each_index { |i| ... } was unresolved ("cannot resolve call to
'each_index'"). Add it alongside each_with_index: yield each index
0..length-1 (only the index bound, no element), for any typed array.
[10, 20, 30].each_index { |i| puts i } # => 0 1 2 (was: unresolved)
This completes #1240 (G2, values_at with a Range argument, landed in the range-args cluster).
Fixes #1240
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 1e6cd661640f
- Parent
- b6110d8a8703
spinel_codegen.rbmodifiedView file
@@ -37642,6 +37642,13 @@
3764237642
end3764337643
end3764437644
37645+
if mname == "each_index"37646+
if @nd_block[nid] >= 037647+
compile_each_index_block(nid)37648+
return 137649+
end37650+
end37651+
3764537652
# `hash.each_value { |v| ... }` for poly_poly_hash. Other hash3764637653
# shapes can be added here once they surface; the underlying3764737654
# representation is the same `vals[order[i]]` walk.@@ -41901,6 +41908,35 @@
4190141908
@in_loop = old4190241909
end4190341910
41911+
# Array#each_index { |i| ... }: yield each index 0..len-1. Like41912+
# each_with_index but with only the index bound (no element). Returns41913+
# the receiver in CRuby; the statement form discards that.41914+
def compile_each_index_block(nid)41915+
old = @in_loop41916+
@in_loop = 141917+
rt = infer_type(@nd_receiver[nid])41918+
rc = compile_expr_gc_rooted(@nd_receiver[nid])41919+
bp1 = get_block_param(nid, 0)41920+
if bp1 == ""41921+
bp1 = "_i"41922+
end41923+
tmp = new_temp41924+
pfx = array_c_prefix(rt)41925+
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")41926+
emit(" mrb_int lv_" + bp1 + " = " + tmp + ";")41927+
@indent = @indent + 141928+
push_scope41929+
declare_var(bp1, "int")41930+
redo_label = push_redo_label41931+
emit_redo_label(redo_label)41932+
compile_stmts_body(@nd_body[@nd_block[nid]])41933+
pop_redo_label41934+
pop_scope41935+
@indent = @indent - 141936+
emit(" }")41937+
@in_loop = old41938+
end41939+
4190441940
def compile_group_by_each_fused(each_nid, gb_nid)4190541941
@needs_gc = 14190641942
arr_nid = @nd_receiver[gb_nid]test/array_each_index.rbaddedView file
Loading diff…
test/array_each_index.rb.expectedaddedView file
Loading diff…