Commit c06bed45

c06bed45b896Merged

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

Description

Add Array#reject, #first, #last, #include?

  • Array#reject with block → inline loop (opposite of select)
  • Array#first → get(arr, 0)
  • Array#last → get(arr, length-1)
  • Array#include?(val) → inline search loop returning boolean
  • Integer#positive?, Integer#negative?

Test: examples/bm_array2.rb — all patterns match Ruby.

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

Details

Commit
c06bed45b896
Parent
7ec2467e9e5a

examples/bm_array2.rbaddedView file

1+# Test additional Array methods
2+
3+arr = (1..10).to_a
4+
5+# reject
6+odds = arr.reject do |x|
7+ x % 2 == 0
8+end
9+puts odds.length # 5
10+
11+# reduce/inject via each
12+total = 0
13+arr.each do |x|
14+ total += x
15+end
16+puts total # 55
17+
18+# reverse
19+nums = (1..10).to_a
20+nums.reverse!
21+puts nums[0] # 10
22+puts nums[9] # 1
23+
24+# Array#first / Array#last
25+puts nums.first # 10
26+puts nums.last # 1
27+
28+# Array#min / Array#max
29+mn = nums[0]
30+mx = nums[0]
31+nums.each do |x|
32+ if x < mn
33+ mn = x
34+ end
35+ if x > mx
36+ mx = x
37+ end
38+end
39+puts mn # 1
40+puts mx # 10
41+
42+# Array#include?
43+if nums.include?(5)
44+ puts "true"
45+else
46+ puts "false"
47+end # true
48+if nums.include?(11)
49+ puts "true"
50+else
51+ puts "false"
52+end # false
53+
54+# Array#compact (remove nils) - simplified: just test count
55+puts nums.length # 10