From: Yohanes Santoso Date: 2005-12-29T23:37:59+09:00 Subject: Re: What is the difference between :foo and "foo" ? Eero Saynatkari writes: > Yohanes Santoso wrote: >> Alex Knaub writes: >> >>> 2005/12/28, Surgeon : >>>> Hi, >>>> >>>> I am a Ruby newbie. I wish I didn't post such a simple question here >>>> but I had to. >>>> What is the difference between :foo (a keyword) and "foo"(a string). >>>> Can they be used interchangeably? Are they fundamentally same and is >>>> the only difference performance? >>> >>> http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red >> >> What a coincidence. Seems like Jim and I finally had enough of people >> conflating symbols and immutable strings on the same day. >> >> http://microjet.ath.cx/WebWiki/2005.12.27_UsingSymbolsForTheWrongReason.html > > While, technically speaking, both of you are absolutely and > undeniably correct (with one correction: Symbols are not Strings), > such a conflation is the best way to get someone over the > initial confusion. The initial confusion is simply part of the learning curve rubyists must go through along with learning that 'everything' is an object. Dumbing down the situation can bring unexpected result: Since we are around Christmas, I'm going to use Santa Claus as an example. To prevent the initial confusion of children finding presents magically appearing before Christmas tree, parents resort to such outlandish notion that that is the work of a joyful guy who has never heard of razor in a red jumper suit and riding reinders-pulled sleigh around the world in one night. Some children outgrew that notion. Some don't. Those that don't would either sulk for a long time about the demise of such a joyful guy, or start to think their parents are liars and spend the next untold years of their life learning enough Math and Physics to prove that it is impossible for a guy like Santa Claus to visit all children in one night. > As this thread quite well demonstrates, > a definition for Symbols is quite difficult to come up with. A formal definition is difficult to formulate correctly, but what CLHS has is good enough: "Symbols are used for their object identity to name various entities in Common Lisp, including (but not limited to) linguistic entities such as variables and functions." -- http://www.lisp.org/HyperSpec/Body/syscla_symbol.html#symbol It is just a way to name/identify things. In RL, you name/identify that collection of atoms 'bird', and another collection of atoms 'cow'. In ruby, you name/identify an object 'host', and another object 'port'. Just as you name parts of collection of atom that you named 'bird' 'wings', 'legs', 'feathers', etc., you name parts of a Hashtable that you named 'ServerConfig' 'host', 'port'. Just as one does not dumbed down "'everything' is an object in ruby", which is a powerful ability, one should not dumbed down "symbols are user-defined identifiers". It's just parts of learning curve. > To paraphrase fifteen thousand fourty-three mediocre > comedians over the last three centuries: > > "A Symbol is like a word, a sentence, a phrase, a > description or, perhaps, a name. Except sometimes." > >> YS. > > > E > > -- > Posted via http://www.ruby-forum.com/.