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

Same use (albeit with less nice syntax) as the ruby symbols.

Many frameworks based on ruby (like Rails) make heavy use of symbols.

They are handy as they are immutable, very cheaply comparable for equality and (in ruby) very light on syntax and they can be made out of thin air whenever you need one.

    some_array[:foo] = 'bar'
instead of

    FOO=0
    some_array[FOO] = 'bar';
(think how error prone it's going to be to add more keys like this. Forgotten declarations, duplication declarations and so on).

You could use strings, but they are often heap-allocated (slow) and sometimes (ruby) even mutable and thus way more expensive to compare for equality and require some kind of hashing to put them as keys into hash tables. Symbols don't. And never conflict.



My idea of them was, they are like string-identifiers, but instead of creating a new string for every string-literal the symbol-literal creates one global, immutable instance.




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

Search: