From: Gary Wright Date: 2008-01-28T23:40:48+09:00 Subject: Re: Symbol#empty? ? On Jan 28, 2008, at 2:50 AM, Dave Thomas wrote: > Symbol tables are fundamentally an optimization: a way of looking > up an identifier. That's why the predominant use of symbols in Ruby > (post Rails) is as hash keys, where they serve the same function. > What function does :"ab" serve for Ruby programmers?(*) And > is the benefit of having it worth the inconsistencies and security > issues involved. By inconsistencies, I assume you mean that s.to_sym works even if the string doesn't represent a valid Ruby identifier? Not sure what security issues you are referring to. > Why not instead say that Symbol is for representing symbols, and > have #intern complain if the corresponding strings isn't valid? > That would allow :"+(binary)" to make sense, as the underlying > symbol is the same as :+. And then if some application in future > has an obscure need to represent the works of Shakespeare as a > symbol, they can simply use a hash like everyone else would... :) String#to_sym is a guaranteed 'perfect' hash though, not a regular hash with possible clashes. If Symbol/String aren't going to provide that functionality then someone is going to immediately write a gem/ extension that does. I could understand having a String/Symbol conversion that complained about illegal identifiers. That could be #intern but that would break backward compatibility, which is another reason not to make #intern/#to_sym more strict than they are. The only reason that "+", "+(binary)", "@+", and so on aren't creating strange problems now is the obscure nature of the synonyms. > (*) orthogonality is not an argument when it comes to Ruby :) But it isn't irrelevant either. You seem to be arguing that just because one particular Ruby implementation needs to map "+" and "@+" to a common hash value and also needs to forbid non-identifier strings that all Ruby applications should accept that quirk in their use of Symbol-like hashing *or* that they should roll their own Symbol implementation (which of course wouldn't have the benefit of Symbol literals). I would argue that there should be some sort of pure Symbol implementation as the 'primitive' functionality. You can build 'synonyms' on top of that implementation if you need them. You can't do it the other way around. Gary Wright