Commit 6858360b

6858360b70dfMerged

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

Description

Add keyword arguments and rest/splat parameters

Keyword args: def greet(name:, greeting: "Hello")

  • RequiredKeywordParameterNode / OptionalKeywordParameterNode parsed
  • Call sites with KeywordHashNode: match keys to params by name
  • Missing optional keywords filled with defaults
  • Type inference from call-site keyword values

Rest/splat: def sum(*nums)

  • RestParameterNode parsed, rest param typed as sp_IntArray
  • Call sites collect extra positional args into IntArray
  • Function body uses rest param as normal array (each, length)

Test: examples/bm_kwargs.rb — keyword calls and splat, matches Ruby. All regressions pass.

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

Details

Commit
6858360b70df
Parent
883d2093a2a0

examples/bm_kwargs.rbaddedView file

1+# Test keyword arguments and splat
2+
3+def greet(name:, greeting: "Hello")
4+ puts greeting
5+ puts name
6+end
7+
8+greet(name: "world")
9+greet(name: "Ruby", greeting: "Hi")
10+
11+# Rest args (splat)
12+def sum(*nums)
13+ total = 0
14+ nums.each do |n|
15+ total += n
16+ end
17+ total
18+end
19+
20+puts sum(1, 2, 3) # 6
21+puts sum(10, 20) # 30