Commit 5aa336d6

5aa336d6ea70Merged

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

Description

Add Array#count (with/without block), min_by, max_by, sort_by for IntArray; count (with/without block) for StrArray

Block params in StrArray blocks are now properly typed as STRING for type inference, enabling chained method calls like w.length > 3.

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

Details

Commit
5aa336d6ea70
Parent
2b3a6d2417a3

examples/bm_array3.rbaddedView file

1+# Test additional array methods
2+
3+arr = Array.new
4+arr.push(5); arr.push(3); arr.push(8); arr.push(1)
5+arr.push(4); arr.push(2); arr.push(7); arr.push(6)
6+
7+# count with block
8+puts arr.count { |x| x > 4 } # 4 (5,8,7,6)
9+
10+# count without block
11+puts arr.count # 8
12+
13+# min_by / max_by
14+puts arr.min_by { |x| x } # 1
15+puts arr.max_by { |x| x } # 8
16+
17+# sort_by
18+sorted = arr.sort_by { |x| -x }
19+puts sorted[0] # 8
20+puts sorted[1] # 7
21+puts sorted[7] # 1
22+
23+# StrArray count
24+words = "hello world foo bar".split(" ")
25+puts words.count { |w| w.length > 3 } # 2 (hello, world)
26+puts words.count # 4
27+
28+puts "done"