Skip to content

Commit aab64ed

Browse files
committed
Add Constants section in inline.md
1 parent 38bcc0c commit aab64ed

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

docs/inline.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,67 @@ Instance variable declarations must be under the `class`/`module` syntax, and th
388388
### Current Limitations
389389

390390
- Only instance variables of class/module instances are allowed
391+
392+
## Constants
393+
394+
Constants are supported by inline RBS declaration.
395+
396+
```ruby
397+
Foo = 123
398+
399+
module Bar
400+
Baz = [1, ""] #: [Integer, String]
401+
end
402+
403+
# Version of the library
404+
#
405+
VERSION = "1.2.3".freeze #: String
406+
```
407+
408+
### Type Inference for Literal Constants
409+
410+
The types of constants may be automatically inferred when the right-hand side consists of literals:
411+
412+
- **Integers**: `COUNT = 42``Integer`
413+
- **Floats**: `RATE = 3.14``Float`
414+
- **Booleans**: `ENABLED = true``bool`
415+
- **Strings**: `NAME = "test"``String`
416+
- **Symbols**: `STATUS = :ready``:ready`
417+
418+
```ruby
419+
MAX_SIZE = 100 # Inferred as Integer
420+
PI = 3.14159 # Inferred as Float
421+
DEBUG = false # Inferred as bool
422+
APP_NAME = "MyApp" # Inferred as String
423+
DEFAULT_MODE = :strict # Inferred as :strict
424+
```
425+
426+
### Explicit Type Annotations
427+
428+
For more complex types or when you want to override inference, use the `#:` syntax:
429+
430+
```ruby
431+
ITEMS = [1, "hello"] #: [Integer, String]
432+
CONFIG = { name: "app", version: 1 } #: { name: String, version: Integer }
433+
CALLBACK = -> { puts "done" } #: ^() -> void
434+
```
435+
436+
### Documentation Comments
437+
438+
Comments above constant declarations become part of the constant's documentation:
439+
440+
```ruby
441+
# The maximum number of retries allowed
442+
# before giving up on the operation
443+
MAX_RETRIES = 3
444+
445+
# Application configuration loaded from environment
446+
#
447+
# This hash contains all the runtime configuration
448+
# settings for the application.
449+
CONFIG = load_config() #: Hash[String, untyped]
450+
```
451+
452+
### Current Limitations
453+
454+
- Module/class aliases are not supported

0 commit comments

Comments
 (0)