From: Jim Weirich Date: 2005-03-19T00:50:38+09:00 Subject: Re: Symbol vs String Sam Roberts said: > Quoting jim@weirichhouse.org, on Thu, Mar 17, 2005 at 01:36:39PM +0900: >> On Wednesday 16 March 2005 09:29 pm, Sam Roberts wrote: >> > Quoting jim@weirichhouse.org, on Thu, Mar 17, 2005 at 11:08:59AM >> +0900: >> > > Use Strings for their content. Use Symbols for their arbitrary >> > > uniqueness. >> > >> > I used to do this, but ran into problems. >> [...] >> > :octet-stream >> > >> > NameError: undefined local variable or method `stream' for >> main:Object >> > from (irb):3 >> > 'octet-stream'.intern >> > ==>:"octet-stream" >> >> Why couldn't you do :octet_stream ? If your answer is because the dash >> comes >> from outside ruby, then I would suggest that the content ("_" vs "-") is >> important ... indicating that you should use strings. > > Maybe I don't know what you mean by "arbitrarily unique". > > "_" vs "-" is no more (or less) important than "a" vs. "z". If the choice if symbol names is arbitrary, then I can change the name of the symbol everywhere that references it without changing the semantics of the program. For example, if any of the following choices are equally valid: :octetstream, :OctetStream, :octet_stream, :stream_of_octets, :octets, :fido, then the choice of name is arbitrary. Of course, some choices are more transparent and convey meaning better, but the program will still work even if we call the symbol :xyzzy. That's what it means to be arbitrary. If the choice of letters is constrained by some outside force, then it is not arbitrary. For example, it might come to you as an attribute in an XML message. Or perhaps you need to write it to a file, and other programs expect that exact sequence of strings. In all these cases, the content (sequence of letters) is important and cannot be changed without breaking the program. When the content of the item is important, use a string. -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)