6868Rest parameters collect multiple arguments into an array. Type the array's element type:
6969
7070``` trb title="rest.trb"
71- def sum(*numbers: Integer): Integer
71+ def sum(*numbers: Array< Integer> ): Integer
7272 numbers.reduce(0, :+)
7373end
7474
75- def concat_strings(*strings: String): String
75+ def concat_strings(*strings: Array< String> ): String
7676 strings.join(" ")
7777end
7878
79- def log_messages(level: String, *messages: String): void
79+ def log_messages(level: String, *messages: Array< String> ): void
8080 messages.each do |msg|
8181 puts "[#{level}] #{msg}"
8282 end
@@ -96,7 +96,7 @@ log_messages("INFO", "App started", "Database connected", "Ready")
9696# [INFO] Ready
9797```
9898
99- The type annotation ` *numbers: Integer ` means "zero or more Integer arguments collected into an array".
99+ The type annotation ` *numbers: Array< Integer> ` means "zero or more Integer arguments collected into an array".
100100
101101## Combining Optional and Rest Parameters
102102
@@ -107,7 +107,7 @@ def create_team(
107107 name: String,
108108 leader: String,
109109 active: Boolean = true,
110- *members: String
110+ *members: Array< String>
111111): Team
112112 Team.new(
113113 name: name,
@@ -166,12 +166,12 @@ post2 = create_post(
166166Use double splat ` ** ` to collect keyword arguments into a hash:
167167
168168``` trb title="keyword_rest.trb"
169- def build_query(table: String, **conditions: String | Integer): String
169+ def build_query(table: String, **conditions: Hash<Symbol, String | Integer> ): String
170170 where_clause = conditions.map { |k, v| "#{k} = #{v}" }.join(" AND ")
171171 "SELECT * FROM #{table} WHERE #{where_clause}"
172172end
173173
174- def create_config(env: String, **options: String | Integer | Boolean): Config
174+ def create_config(env: String, **options: Hash<Symbol, String | Integer | Boolean> ): Config
175175 Config.new(environment: env, options: options)
176176end
177177
@@ -190,7 +190,7 @@ config = create_config(
190190)
191191```
192192
193- The type annotation ` **conditions: String | Integer ` means "zero or more keyword arguments with String or Integer values collected into a hash".
193+ The type annotation ` **conditions: Hash<Symbol, String | Integer> ` means "zero or more keyword arguments with String or Integer values collected into a hash".
194194
195195## Required Keyword Arguments
196196
@@ -238,10 +238,10 @@ You can combine all parameter types, but they must follow this order:
238238def complex_function(
239239 required_pos: String, # 1. Required positional
240240 optional_pos: Integer = 0, # 2. Optional positional
241- *rest_args: String, # 3. Rest parameter
241+ *rest_args: Array< String>, # 3. Rest parameter
242242 required_kw: Boolean, # 4. Required keyword
243243 optional_kw: String = "default", # 5. Optional keyword
244- **rest_kwargs: String | Integer # 6. Keyword rest
244+ **rest_kwargs: Hash<Symbol, String | Integer> # 6. Keyword rest
245245): Hash<String, String | Integer | Boolean>
246246 {
247247 "required_pos" => required_pos,
@@ -283,7 +283,7 @@ class HTTPRequestBuilder
283283 end
284284
285285 # Required + rest parameters
286- def delete(*urls: String): Array<Response>
286+ def delete(*urls: Array< String> ): Array<Response>
287287 urls.map { |url| make_request("DELETE", url, nil, {}) }
288288 end
289289
@@ -302,7 +302,7 @@ class HTTPRequestBuilder
302302 def custom_request(
303303 method: String,
304304 url: String,
305- **headers: String
305+ **headers: Hash<Symbol, String>
306306 ): Response
307307 make_request(method, url, nil, headers)
308308 end
@@ -373,18 +373,18 @@ class Logger
373373 end
374374
375375 # Multiple messages with rest parameter
376- def log_many(level: String, *messages: String): void
376+ def log_many(level: String, *messages: Array< String> ): void
377377 messages.each { |msg| log(msg, level) }
378378 end
379379
380380 # Structured logging with keyword rest
381- def log_structured(message: String, **metadata: String | Integer | Boolean): void
381+ def log_structured(message: String, **metadata: Hash<Symbol, String | Integer | Boolean> ): void
382382 meta_str = metadata.map { |k, v| "#{k}=#{v}" }.join(" ")
383383 puts "[INFO] #{message} | #{meta_str}"
384384 end
385385
386386 # Flexible debug logging
387- def debug(*messages: String, **context: String | Integer): void
387+ def debug(*messages: Array< String> , **context: Hash<Symbol, String | Integer> ): void
388388 messages.each do |msg|
389389 ctx_str = context.empty? ? "" : " (#{context.map { |k, v| "#{k}=#{v}" }.join(", ")})"
390390 puts "[DEBUG] #{msg}#{ctx_str}"
454454### Variadic Factory Functions
455455
456456``` trb title="factory.trb"
457- def create_users(*names: String, role: String = "user"): Array<User>
457+ def create_users(*names: Array< String> , role: String = "user"): Array<User>
458458 names.map { |name| User.new(name: name, role: role) }
459459end
460460
@@ -464,7 +464,7 @@ users = create_users("Alice", "Bob", "Charlie", role: "admin")
464464### Configuration Merging
465465
466466``` trb title="config.trb"
467- def merge_config(base: Hash<String, String>, **overrides: String): Hash<String, String>
467+ def merge_config(base: Hash<String, String>, **overrides: Hash<Symbol, String> ): Hash<String, String>
468468 base.merge(overrides)
469469end
470470
@@ -480,8 +480,8 @@ config = merge_config(
480480Optional and rest parameters give your functions flexibility while maintaining type safety:
481481
482482- ** Optional parameters** (` param: Type = default ` ) have default values
483- - ** Rest parameters** (` *args: Type ` ) collect multiple arguments into an array
484- - ** Keyword rest** (` **kwargs: Type ` ) collects keyword arguments into a hash
483+ - ** Rest parameters** (` *args: Array< Type> ` ) collect multiple arguments into an array
484+ - ** Keyword rest** (` **kwargs: Hash<Symbol, Type> ` ) collects keyword arguments into a hash
485485- T-Ruby ensures type safety for all parameter variations
486486
487487Master these patterns to write flexible, type-safe APIs that are pleasant to use.
0 commit comments