https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112013-02-07T16:16:59ZRuby Issue Tracking SystemRuby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=359672013-02-07T16:16:59Zalexeymuranov (Alexey Muranov)
<ul></ul><p>In my opinion, it would be more useful to have a method that checks if a given string matches one of symbols in a given set. It is hard for me to think of a situation where one needs to know if 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. In fact, something like String#to_one_of_symbols(set), which returns nil if no matching symbol found, would seem enough for most purposes.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=359682013-02-07T16:46:26Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I think also that finding a symbol in a set is related to (can be used in) Hash With Indifferent Access. HWIA are implemented in RoR and Sinatra in different ways. Would be nice if Ruby itself had Hash With Indifferent Access.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=359692013-02-07T17:53:11Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>On 7 February 2013 17:17, alexeymuranov (Alexey Muranov) wrote:</p>
<blockquote>
<p>In my opinion, it would be more useful to have a method that checks if a<br>
given string matches one of a symbols in a given set. It is hard for me<br>
to think of a situation where one needs to know is a string matches any<br>
of the created symbols whatsoever. Similarly, instead of<br>
#to_existing_symbol, it seems to me useful to have a method that<br>
efficiently finds a symbol in a set by its string representation.</p>
</blockquote>
<p>That would be a property of the set rather than of Symbol, and can easily<br>
be achieved by constructing a Hash whose values are the Symbols in question<br>
and keys are the .to_s of the values. e.g.</p>
<pre><code> 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
</code></pre>
<p>I believe the original feature request is more useful, because while it is<br>
trivial to construct the above class (or an improved version) it is much<br>
more difficult to manage membership, especially when you care about "the<br>
set of all symbols", which is a legitimate concern in the case of the DOS<br>
attack mentioned in the original request. Since the set of all symbols is<br>
already available to Symbol (as Symbol.all_symbols), that seems the logical<br>
place to implement the feature, if at all.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=360222013-02-08T07:59:16ZStudent (Nathan Zook)blogger@pierian-spring.net
<ul></ul><p>When methods can be dynamically generated, things get hairy. For instance, active record 1 & 2 defined > n! dynamic finders for each model where n is the number of columns in the model. These methods are never all generated at once, but they can be.</p>
<p>Suppose one were to pass options to the to_json method that serializes the return data. In rails, any argumentless method on any object attached through any relationship chain can be valid. That's a lot to monitor.</p>
<p>Once the hash is parsed, checks can be made to see if we want to allow the method to be called. But that is only after the symbols have been created.</p>
<p>Thus the desire to check to see if the proposed symbol is new before creating it.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=367372013-02-22T09:08:42Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=367642013-02-22T12:17:40Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I agree with the basic concept of the proposal.<br>
I am not sure Symbol#defined? is a appropriate name for it yet.</p>
<p>The possible addition I like is either:</p>
<ul>
<li>add Symbol#define? or similar method</li>
<li>add optional keyword argument to intern e.g. "foo".intern(exist: true)</li>
</ul>
<p>Matz.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=367752013-02-22T14:52:45ZStudent (Nathan Zook)blogger@pierian-spring.net
<ul></ul><p>These sound like my (new & preferred) proposal for Symbol[string] <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: New method Symbol[string] (Closed)" href="https://bugs.ruby-lang.org/issues/7854">#7854</a>.<br>
That is, return the symbol if it already exists, nil if not.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=367792013-02-22T16:13:33Zvo.x (Vit Ondruch)v.ondruch@tiscali.cz
<ul></ul><p>Student (Nathan Zook) wrote:</p>
<blockquote>
<p>#2) to_existing_sym. This would be defined in the same places as to_sym, but would through an argument error if the symbol did not already exist.</p>
</blockquote>
<p>Reading the documentation, it says "Returns the Symbol corresponding to str, creating the symbol if it did not previously exist." So what would this method did different? Or is the documentation wrong?</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=367832013-02-22T18:59:12Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>On 22 February 2013 17:13, vo.x (Vit Ondruch) <a href="mailto:v.ondruch@tiscali.cz" class="email">v.ondruch@tiscali.cz</a> wrote:</p>
<blockquote>
<h2>Reading the documentation, it says "Returns the Symbol corresponding to<br>
str, creating the symbol if it did not previously exist." So what would<br>
this method did different? Or is the documentation wrong?</h2>
</blockquote>
<p>Existing method:<br>
to_sym: Returns the Symbol corresponding to str, creating the symbol if<br>
it did not previously exist.</p>
<p>New (proposed) method:<br>
to_existing_sym: Returns the Symbol corresponding to str, raising an<br>
Exception if it did not previously exist.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=373352013-03-07T06:59:42Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>I agree with the basic concept of the proposal.<br>
I am not sure Symbol#defined? is a appropriate name for it yet.</p>
<p>The possible addition I like is either:</p>
<ul>
<li>add Symbol#define? or similar method</li>
<li>add optional keyword argument to intern e.g. "foo".intern(exist: true)</li>
</ul>
<p>Matz.</p>
</blockquote>
<p>My ruby core abilities are somewhat limited as yet, but in experimentation on a local fork I have implemented "foo".to_existing_sym (which raises an error) and "foo".interned (which returns nil); <a href="https://gist.github.com/phluid61/5086304" class="external">https://gist.github.com/phluid61/5086304</a></p>
<p>My next goal, now that I have some familiarity in this area, would be to instead extend the existing rb_str_intern to accept the 'exist' keyword argument.</p>
<p>I assume it's ok that to_sym also accepts the kwarg?</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=373362013-03-07T08:33:00ZStudent (Nathan Zook)blogger@pierian-spring.net
<ul></ul><p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>I agree with the basic concept of the proposal.<br>
I am not sure Symbol#defined? is a appropriate name for it yet.</p>
<p>The possible addition I like is either:</p>
<ul>
<li>add Symbol#define? or similar method</li>
<li>add optional keyword argument to intern e.g. "foo".intern(exist: true)</li>
</ul>
<p>Matz.</p>
</blockquote>
<p>My ruby core abilities are somewhat limited as yet, but in experimentation on a local fork I have implemented "foo".to_existing_sym (which raises an error) and "foo".interned (which returns nil); <a href="https://gist.github.com/phluid61/5086304" class="external">https://gist.github.com/phluid61/5086304</a></p>
<p>My next goal, now that I have some familiarity in this area, would be to instead extend the existing rb_str_intern to accept the 'exist' keyword argument.</p>
<p>I assume it's ok that to_sym also accepts the kwarg?</p>
</blockquote>
<p>I agree with Matz that the names are problematic. What about Symbol[] ? (<a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: New method Symbol[string] (Closed)" href="https://bugs.ruby-lang.org/issues/7854">#7854</a>)</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=374212013-03-09T20:59:57Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>Student (Nathan Zook) wrote:</p>
<blockquote>
<p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>I agree with the basic concept of the proposal.<br>
I am not sure Symbol#defined? is a appropriate name for it yet.</p>
<p>The possible addition I like is either:</p>
<ul>
<li>add Symbol#define? or similar method</li>
<li>add optional keyword argument to intern e.g. "foo".intern(exist: true)</li>
</ul>
<p>Matz.</p>
</blockquote>
<p>My ruby core abilities are somewhat limited as yet, but in experimentation on a local fork I have implemented "foo".to_existing_sym (which raises an error) and "foo".interned (which returns nil); <a href="https://gist.github.com/phluid61/5086304" class="external">https://gist.github.com/phluid61/5086304</a></p>
<p>My next goal, now that I have some familiarity in this area, would be to instead extend the existing rb_str_intern to accept the 'exist' keyword argument.</p>
<p>I assume it's ok that to_sym also accepts the kwarg?</p>
</blockquote>
<p>I agree with Matz that the names are problematic. What about Symbol[] ? (<a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: New method Symbol[string] (Closed)" href="https://bugs.ruby-lang.org/issues/7854">#7854</a>)</p>
</blockquote>
<p>Having experimented with multiple implementations ( e.g. <a href="https://gist.github.com/phluid61/5104973" class="external">https://gist.github.com/phluid61/5104973</a> ) I agree that Symbol[] does seem like a much more useful, all-encompassing method.</p> Ruby master - Feature #7795: Symbol.defined? and/or to_existing_symbolhttps://bugs.ruby-lang.org/issues/7795?journal_id=688812017-12-25T18:15:11Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul>