From: Matthew Kerwin Date: 2013-02-07T17:49:42+09:00 Subject: [ruby-core:51970] Re: [ruby-trunk - Feature #7795] Symbol.defined? and/or to_existing_symbol --f46d042dfd1989e1dd04d51e82e7 Content-Type: text/plain; charset=ISO-8859-1 On 7 February 2013 17:17, alexeymuranov (Alexey Muranov) wrote: > In my opinion, it would be more useful to have a method that checks if a > given string matches one of a symbols in a given set. It is hard for me > to think of a situation where one needs to know is a string matches any > of the created symbols whatsoever. Similarly, instead of > #to_existing_symbol, it seems to me useful to have a method that > efficiently finds a symbol in a set by its string representation. That would be a property of the set rather than of Symbol, and can easily be achieved by constructing a Hash whose values are the Symbols in question and keys are the .to_s of the values. e.g. class SymbolSet def initialize @hash = {} end def <<(sym) @hash[sym.to_s] = sym end def defined?(s) @hash.has_key? s.to_s end def existing_sym(s) @hash[s.to_s] or raise "symbol :'#{s}' not defined in this set" end end I believe the original feature request is more useful, because while it is trivial to construct the above class (or an improved version) it is much more difficult to manage membership, especially when you care about "the set of all symbols", which is a legitimate concern in the case of the DOS attack mentioned in the original request. Since the set of all symbols is already available to Symbol (as Symbol.all_symbols), that seems the logical place to implement the feature, if at all. --f46d042dfd1989e1dd04d51e82e7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 7 February 2013 17:17, alexeymurano= v (Alexey Muranov) wrote:
> In my opinion, it would be more useful to= have a method that checks if a
> given string matches one of a symbo= ls in a given set.=A0 It is hard for me
> to think of a situation where one needs to know is a string matches an= y
> of the created symbols whatsoever. Similarly, instead of
> = #to_existing_symbol, it seems to me useful to have a method that
> ef= ficiently finds a symbol in a set by its string representation.

That would be a property of the set rather than of Symbol, and ca= n easily be achieved by constructing a Hash whose values are the Symbols in= question and keys are the .to_s of the values.=A0 e.g.

=A0=A0= =A0 class SymbolSet
=A0=A0=A0=A0=A0 def initialize
=A0=A0=A0=A0=A0=A0= =A0 @hash =3D {}
=A0=A0=A0=A0=A0 end
=A0=A0=A0= =A0=A0 def <<(sym)
=A0=A0=A0=A0=A0=A0=A0 @hash[sym.to_s= ] =3D sym
=A0=A0=A0=A0=A0 end
=A0=A0=A0=A0=A0 d= ef defined?(s)
=A0=A0=A0=A0=A0=A0=A0 @hash.has_key? s.to_s
=A0=A0= =A0=A0=A0 end
=A0=A0=A0=A0=A0 def existing_sym(s)
=A0=A0=A0=A0=A0=A0=A0 @hash[s.to_s] or raise "symbol :'#{s}'= ; not defined in this set"
=A0=A0=A0=A0=A0 end
=A0=A0=A0 end

I believe the origi= nal feature request is more useful, because while it is trivial to construc= t the above class (or an improved version) it is much more difficult to man= age membership, especially when you care about "the set of all symbols= ", which is a legitimate concern in the case of the DOS attack mention= ed in the original request.=A0 Since the set of all symbols is already avai= lable to Symbol (as Symbol.all_symbols), that seems the logical place to im= plement the feature, if at all.
--f46d042dfd1989e1dd04d51e82e7--