From: Alex Combas Date: 2006-01-09T18:01:50+09:00 Subject: rue teaches piglet about symbols on #ruby-lang Taken from #ruby-lang with a slight cleanup (I am piglet) * piglet wonders if someone would be so kind to explain what a symbol is slowly and clearly so his simple pig brain will understand. "A symbol is the name of an object" just doesn't grok. dagbrown: It's a name without an object attached to it. rue: Oo, let me try my state again! rue: It is a name or description that is only meaningful to the programmer rue: Like you can say @status = :connected rue: The :connected does not mean anything to ruby, only you. You have decided to call this particular status :connected * piglet tries hard not to look confused. piglet: how does this fit in with .to_sym or .to_i then? rue: Those are just ways to construct (or deconstruct) a symbol rue: The implementation is secondary and has no inherent meaning rue: So long as you understand what symbols are for (:connected), you are golden. Then you can take a look at implementation details piglet: ah seems clearer now.. still fuzzy though.. rue: Is there a particular usage that is confusing? piglet: i'll get back to you, let me meditate some more piglet: i like playing with things in irb until i understand, but these symbols are weird rue: One more! You could just as well write attr_accessor 'foo' . You are just saying the name of the method you want to create. rue: All these people with their 'immediate values' and 'integer representations' are out to confuse people, I think :) piglet: someone should let them know they've won, so they will stop rue: Yep. But do you, sort of, see I what I am saying? They are just names that are meaningful to YOU. Instead of having to say @status = 1, you can say @status = :connected Pieter: symbols are like integers you pass to a method to select a state or something. With symbols you can use human-readable objects instead of integers * rue hopes for a tentative 'yes' rue: That would mean we are about 8000 lines of text better than ruby-talk :) piglet: rue I get it :) Pieter: wow :) rue: Yays :P piglet: are symbols more better, or just 'more meaningful'? Pieter: you should post the log to ruby-talk and pimp rue: More meaningful. rue: As I mentioned, there are some implementation details that may be interesting in the current ruby. * piglet is satisfied :) rue: The thing that is mostly at the root of all this confusion is that ruby itself creates some symbols. Mostly because it is a convenient rue: The second thing --and this is *completely useless knowledge*-- is that symbols are in fact processed as 'immediate values'. piglet: I guess I have to ask what an immediate value is now. Pieter: :) rue: Normal objects are held as references. Say, a = 'foo', a does not actually contain the object, just a reference rue: Fixnums and Symbols are actually stored 'in' the variable (references are integers too). rue: But these are just optimizations. There is no functional reason this is the way it is. piglet: fixnums and symbols are faster then because they are actually stored in the variable and not just being pointed to by the variable? rue: Precisely. piglet: I didnt know ruby stored anything in its variables besides pointer type things rue: Yep --and the important bit is that it does not matter if it does or not. piglet: no it wouldnt matter, but its interesting to know rue: Oh, hell, I will post on ruby-talk. At least they will have something to talk about tomorrow even if it is bashing our methodology :P piglet: rue, would you mind if I do as Pieter says and post this convo to ruby-lang, I bet someone will get something out of it. rue: Oh, sure. Just paste the whole thing all the way down here :) piglet: :) -- Alex Combas http://noodlejunkie.blogspot.com/