News for hackers. Stuff that matters.
Re:Gem adds support for creating state machines for attribut... by nerdponx on Sunday January 21, 2024 @07:31PM (#39084604)
Wait, is :state a keyword or an instance attribute? Using the same syntax for both seems really confusing.
Re:Gem adds support for creating state machines for attribut... by deschutes on Sunday January 21, 2024 @08:50PM (#39085135)
It's a symbol. Basically a glorified string literal.
Re:Gem adds support for creating state machines for attribut... by lloeki on Monday January 22, 2024 @03:20AM (#39087472)
It's more like an integer with a textual name.
Re:Gem adds support for creating state machines for attribut... by vidarh on Sunday January 21, 2024 @10:32PM (#39085885)
Ruby doesn't have "instance attributes". Instance variables are prefixed with "@". Anything prefixed by ":" is a symbol.

In some contexts we use symbols to refer to methods by name.

Anything you might thing is an "attribute" is likely a method. E.g. on some_object.name, name is always a method. You might be confused because e.g. "attr_reader" takes symbol arguments, because it's just another method. E.g. "attr_reader :foo" defines a method named "foo" to return "@foo". ":foo" there is a Symbol representing the name "foo".

Re:Gem adds support for creating state machines for attribut... by nerdponx on Monday January 22, 2024 @09:02AM (#39089622)
So it's just a symbol, but in this context it will be used to look up the "state" method? Not unlike how symbols are sometimes used as "designators" of various things in Lisp?