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 methods2+
3+
arr = (1..10).to_a4+
5+
# reject6+
odds = arr.reject do |x|7+
x % 2 == 08+
end9+
puts odds.length # 510+
11+
# reduce/inject via each12+
total = 013+
arr.each do |x|14+
total += x15+
end16+
puts total # 5517+
18+
# reverse19+
nums = (1..10).to_a20+
nums.reverse!21+
puts nums[0] # 1022+
puts nums[9] # 123+
24+
# Array#first / Array#last25+
puts nums.first # 1026+
puts nums.last # 127+
28+
# Array#min / Array#max29+
mn = nums[0]30+
mx = nums[0]31+
nums.each do |x|32+
if x < mn33+
mn = x34+
end35+
if x > mx36+
mx = x37+
end38+
end39+
puts mn # 140+
puts mx # 1041+
42+
# Array#include?43+
if nums.include?(5)44+
puts "true"45+
else46+
puts "false"47+
end # true48+
if nums.include?(11)49+
puts "true"50+
else51+
puts "false"52+
end # false53+
54+
# Array#compact (remove nils) - simplified: just test count55+
puts nums.length # 10src/codegen.cmodifiedView file
Loading diff…