Skip to content

Commit 4ac2cef

Browse files
committed
Add Attributes section
1 parent ab28a38 commit 4ac2cef

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

docs/inline.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,80 @@ end
189189
- Class methods and singleton methods are not supported
190190
- Parameter types are not supported with doc-style syntax
191191
- Method visibility declaration is not supported yet
192+
193+
## Attributes
194+
195+
Inline RBS supports Ruby's attribute methods: `attr_reader`, `attr_writer`, and `attr_accessor`.
196+
197+
```ruby
198+
class Person
199+
attr_reader :name #: String
200+
attr_writer :age #: Integer
201+
attr_accessor :email #: String?
202+
end
203+
```
204+
205+
It detects these attribute declarations and generates the corresponding getter and setter methods.
206+
207+
The accessor methods and instance variables are defined.
208+
209+
### Unannotated attributes
210+
211+
Attributes defined without type annotations are treated as `untyped`:
212+
213+
```ruby
214+
class Person
215+
attr_reader :name
216+
attr_writer :age
217+
attr_accessor :email
218+
end
219+
```
220+
221+
### Type annotations for attributes
222+
223+
You can add type annotations to attributes using the `#:` syntax in trailing comments:
224+
225+
```ruby
226+
class Person
227+
attr_reader :name #: String
228+
attr_writer :age #: Integer
229+
attr_accessor :email #: String?
230+
end
231+
```
232+
233+
This generates the following typed methods:
234+
- `name: () -> String`
235+
- `age=: (Integer) -> Integer`
236+
- `email: () -> String?` and `email=: (String?) -> String?`
237+
238+
### Multiple attributes
239+
240+
When declaring multiple attributes in one line, the type annotation applies to all attributes:
241+
242+
```ruby
243+
class Person
244+
attr_reader :first_name, :last_name #: String
245+
attr_accessor :age, :height #: Integer
246+
end
247+
```
248+
249+
All attributes in each declaration share the same type.
250+
251+
### Non-symbol attribute names
252+
253+
Attribute names must be symbol literals.
254+
255+
```ruby
256+
class Person
257+
attr_reader "name" #: String
258+
259+
age = :age
260+
attr_writer age #: Integer
261+
end
262+
```
263+
264+
The attribute definitions are ignored because the names are given by string literals and local variables.
265+
266+
### Current Limitations
267+
268+
- Attribute visibility is not supported yet. All attributes are _public_

0 commit comments

Comments
 (0)