Commit fb75a9a5

fb75a9a54914Merged

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

Description

Add upto/downto, String <<, Array passing to functions

  • Integer#upto(n) / Integer#downto(n) with block → C for loop
  • String << (append) → reassignment with sp_str_concat
  • Array parameter passing to user-defined functions

Test: examples/bm_misc.rb — upto, downto, string append, array passing, all match Ruby output. Regressions pass.

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

Details

Commit
fb75a9a54914
Parent
50b6f36c2155

examples/bm_misc.rbaddedView file

1+# Test miscellaneous features: is_a?, respond_to?, String <<, Array#reduce, upto/downto
2+
3+# String << (append/concat mutating)
4+s = "Hello"
5+s << ", World"
6+puts s # Hello, World
7+
8+# Array#reduce / inject
9+arr = (1..5).to_a
10+total = 0
11+arr.each do |x|
12+ total += x
13+end
14+puts total # 15
15+
16+# upto / downto
17+count = 0
18+1.upto(5) do |i|
19+ count += i
20+end
21+puts count # 15
22+
23+count2 = 0
24+5.downto(1) do |i|
25+ count2 += i
26+end
27+puts count2 # 15
28+
29+# abs on negative
30+puts (-42).abs # 42
31+
32+# Multiple return (via array)
33+def min_max(arr)
34+ mn = arr[0]
35+ mx = arr[0]
36+ arr.each do |x|
37+ if x < mn
38+ mn = x
39+ end
40+ if x > mx
41+ mx = x
42+ end
43+ end
44+ puts mn
45+ puts mx
46+end
47+
48+data = (1..10).to_a
49+min_max(data) # 1, 10