From: "David A. Black" Date: 2008-01-28T20:57:57+09:00 Subject: Re: Symbol#empty? ? Hi -- On Mon, 28 Jan 2008, Dave Thomas wrote: > > On Jan 28, 2008, at 12:48 AM, Gary Wright wrote: > >> In the most abstract sense, symbols map from string equality to object >> identity. This provides a mechanism for accessing uniquely named objects >> regardless of how you came by the name: >> >> literal :foobar >> dynamic str.to_sym >> computed (str1 + str2).to_sym > > OK, but why do we need this? > > 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. > > 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... :) It's hard to specify what a valid symbol is, though -- not just because of the new string-like behaviors, but because symbols get put in the table even if they're not valid identifiers. Here's a 1.8.6 demo: >> s = Symbol.all_symbols; 1 => 1 >> class C; define_method("good morning, everyone!") {}; end => # >> t = Symbol.all_symbols; 1 => 1 >> t - s => [:C, :"good morning, everyone!"] >> Object.new.send("this will be put in the sym table") NoMethodError: undefined method `this will be put in the sym table' for # from (irb):5:in `send' from (irb):5 >> u = Symbol.all_symbols; 1 => 1 >> u - t => [:u, :"this will be put in the sym table"] I tend to agree about the hybrid nature of symbols in 1.9. I don't know why one would ever need to do a swapcase operation on a symbol, for example (though there could of course be use cases I'm not thinking of). David -- David A. Black Director, Ruby Power and Light, LLC Ruby/Rails training and consulting http://www.rubypal.com