Commit 0fa8b592

0fa8b5922e38Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 2 files · +1 added · ~1 modified

Description

Add block_given?, fix block variable capture for writes

  • block_given? → (_block != NULL) in yield functions
  • Yield function calls without blocks pass NULL, NULL
  • Fix scan_captures: writes to outer-scope variables are captures (previously treated as local definitions, breaking the callback)

Category 3 (block completeness) — block_given? done.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
0fa8b5922e38
Parent
583eeebe16e0

examples/bm_block2.rbaddedView file

1+# Test block_given? and optional blocks
2+
3+def maybe_yield(x)
4+ if block_given?
5+ yield x
6+ else
7+ x * 2
8+ end
9+end
10+
11+puts maybe_yield(5) # 10 (no block)
12+
13+result = 0
14+maybe_yield(5) do |n|
15+ result = n * 3
16+end
17+puts result # 15
18+
19+# Method that always yields
20+def each_pair(a, b)
21+ yield a
22+ yield b
23+end
24+
25+total = 0
26+each_pair(10, 20) do |n|
27+ total += n
28+end
29+puts total # 30
30+
31+puts "done"