From: Hal Fulton Date: 2005-03-17T12:02:19+09:00 Subject: Re: Symbol vs String Sam Roberts wrote: > Quoting jim@weirichhouse.org, on Thu, Mar 17, 2005 at 11:08:59AM +0900: > >>On Wednesday 16 March 2005 03:37 pm, Sebesty�n G�bor wrote: >> >>>Just a dumb question: what is the real difference between { :aKey => >>>"aValue" } and { "aKey" => "aValue" } ? I know the first key is a >>>symbol the latter is a string. I like string keys why should I use >>>symbols? Why symbols worth to use as keys? >> >>Use Strings for their content. Use Symbols for their arbitrary uniqueness. > > > I used to do this, but ran into problems. > > Symbols are great for things related to ruby becuase the :bar form for symbol > literals accepts the same kind of chars as ruby identifiers. I use them be > preference in interacting with ruby's meta-programming APIs. > > They start to fall down outside of this. For example, I tried to use > with mime types: > > :text > ==>:text > :video > ==>:video > :octet-stream > NameError: undefined local variable or method `stream' for main:Object > from (irb):3 > 'octet-stream'.intern > ==>:"octet-stream" > > You CAN use them for things outside of the domain of ruby names, but it gets > painful if the names of those things are arbitarily unique, but have "-" > characters in their name, you first have to create a String! > > You can get around this by creating constants: > > OCTETSTREAM = 'octet-stream'.intern > TEXT = :text > > etc., but that might not fit your API goals very well. > > Anyhow, I moved back to using strings instead of symbols. The need to create a > string and intern it for things that are logically symbols but have a "-" in > them was too painful. > > That was my experience, anyhow. I believe you can do things like :"octet-stream" -- but I grant that is not much better. Hal