Thursday, December 06, 2007

What is a Ruby 'symbol'?

A symbol is something that looks like ':symbol' in ruby code, where it is like a variable name but preceded by a colon character. It is used extensively in Rails, which had been confusing me in the past, and it still does, given that I have not grasped full clarity of its significance yet.

The explanation given to me was that the definition of a symbol is 'like a string you never intend to show to the outside world'. The example told to me as an illustration was primarily utilized as hash keys, which I am told that it is cheaper than using strings (probably because it's immutability, which I suspect it's acting like a singleton object too). The closest example that I keep associating to, is the C preprocessor usage of '#define'.

Other than its extensive usage in Rails, I'm don't see how much it will be of any average coding use yet, unfortunately. There's still some way to go in the path of Ruby enlightenment for me.


Post a Comment