Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Does Ruby's Symbol type acceptably accomplish this?


Symbols 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).


I 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.


Debatable. 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.


Yes, that is a valid point, and definitely a downside.


But note that ruby's symbols are interned, so if you create lots of them programatically they never get GC'd.


For 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: