From: Sam Roberts Date: 2005-03-19T02:31:58+09:00 Subject: Re: Symbol vs String Quoting jim@weirichhouse.org, on Sat, Mar 19, 2005 at 12:50:38AM +0900: > > 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. Ah. Then, no, its not really arbitrary. More specifically, I can make it arbitrary, but then I might be forced to make it more and more arbitrary! If I map: x-mailer => :xmailer Then somebody decides to make a header xmailer I have to map: xmailer => :zz_xmailer etc. I guess I could madk a mapping table, hashing strings to symbols, but at this point symbols aren't making my code clearer or easier to use. In the example of mime types, I probably could use abitray symbols. Anybody who decides to make a new mime type called application/octet_stream or application/octet_stream given tha application/octet-stream is a standard name deserves to be publically humiliated. So I could use :octetstream, arbitrarily. I just wanted to use symbols for the efficiency, and to emphasize their uniqueness in terms of case-sensitivity, it seemed to fit, but for serveral reasons I discovered it didn't. Cheers, Sam