3 ms·
Does Ruby's Symbol type acceptably accomplish this?
by tolmasky 13y ago
Does Ruby's Symbol type acceptably accomplish this?
- jerf 13y agoSymbols are yet another use case; "I want something as efficient as a small number" (probably 4 bytes nowadays) "but I want to type a nice human name for it, and I don't want to even have to think about the translation". It's neither "text" nor a "binary", it's actually a well-disguised int (or similar type).
- mratzloff 13y agoI love symbols in Ruby. I wish every language had something similar, rather than having me define constants, importing the constants, etc. Just let me create symbols I can pass around easily.
- mercurial 13y agoDebatable. I haven't been doing Ruby for a few years, but when I did, it was mostly used as dictionary keys. So, sure, you didn't need to define your constant somewhere, but on the other hand, you end up a typo away from an invalid lookup. Getting a NameError is a much easier way to locate the origin of the problem.
- mratzloff 13y agoYes, that is a valid point, and definitely a downside.
- asolove 13y agoBut note that ruby's symbols are interned, so if you create lots of them programatically they never get GC'd.
- asgard1024 13y agoFor some use cases, symbol types are useful (note that they originally come from Lisp). But not all. For example, in many internet protocols, you need computer strings which are not symbols.