<id>https://bugs.ruby-lang.org/</id> <icon>https://bugs.ruby-lang.org/favicon.ico</icon> <updated>2013-02-14T09:49:25Z</updated> <author> <name>Ruby Issue Tracking System</name> </author> <entry> <title>Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=36257 2013-02-14T09:49:25Z Nathan Zook blogger@pierian-spring.net <ul></ul><p>Bad idea. to_str should only be defined on things that really are Strings, and Symbol are most definitely not Strings.</p> <p>I agree that Symbol is unusually close to String. If, for your needs, you were to define to_st on String &amp; on Symbol, you could have the utility you desire.</p> Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=36264 2013-02-14T10:14:53Z Thomas Sawyer <ul></ul><blockquote> <p>If, for your needs, you were to define to_st on String &amp; on Symbol, you could have the utility you desire.</p> </blockquote> <p>Yes, I thought about that. But concluded it was most likely unnecessary complexity when #to_str would work fine.</p> <p>You say &quot;Bad idea&quot;. But show me why it is bad idea other then &quot;them&#39;s the rules&quot;. I tried to think of a problem case, and the only one I can think of is using <code>foo.respond_to?(:to_str)</code> to identify Stringy things and very specifically not meaning to include Symbols. It&#39;s possible, but it&#39;s a fairly narrow proposition. Not the least reason being that one should never use <code>respond_to?</code> if one does not need to b/c it is a fragile approach. But more significantly, what is more likely to be used? This narrow usecase or Symbol#to_str? Clearly the later by far. And the former is easily solved with <code>&amp;&amp; !Symbol === foo</code>.</p> Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=36266 2013-02-14T10:36:37Z Charlie Somerville charliesome@ruby-lang.org <ul></ul><p>Symbols are not Strings. I&#39;m afraid this would only serve to blur the line even more. </p> <p>Rubyists need to stop using Symbols where they actually want a String, and vice versa.</p> <p>Strong -1 from me.</p> Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=36272 2013-02-14T13:08:50Z Eric Hodel drbrain@segment7.net <ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>You cannot gsub, enumerate characters in or alter encoding of a Symbol, so it is not a string representation.</p> Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=36317 2013-02-15T21:24:37Z Thomas Sawyer <ul></ul><blockquote> <p>You cannot gsub, enumerate characters in or alter encoding of a Symbol, so it is not a string representation.</p> </blockquote> <p>That the official spec on the definition of a Stringy-thing? That&#39;s the &quot;problem&quot; with #to_str, #to_ary, etc. isn&#39;t it? There <em>is no</em> absolute interface that dictates their proper use. As long the method returns the expected type then its purely a question of <em>practicality</em>. And I submit that Symobol#to_str is about a practical as it gets.</p> <p>And let me put it another way. If you inherited some code that relied on an object responding to #to_str to ensure it also responded to #gsub, #map and #force_encoding (which is the crux of your &quot;definition&quot;), what would you think? Yes, you&#39;d have seriously fragile code on your hands and you&#39;d be a&#39;fixing it.</p> <p>I think you rejected this issue far too prematurely. Do you guys even know the purpose of dialog?</p> Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=36318 2013-02-15T21:58:02Z Thomas Sawyer <ul></ul><p>=begin<br> @charliesome Actually, that&#39;s exactly what my proposal attempts to address. You don&#39;t always have a choice in what type of object you receive, but you want it to become a string. Consider a DSL like Rake&#39;s. One could use:</p> <pre>task :foo do ... </pre> <p>Or</p> <pre>task &#39;foo&#39; do ... </pre> <p>Either one is acceptable, and I think it would be overreaching to make people not be able to use a symbol here. </p> <p>On the other hand do we want any object to be acceptable? B/c just about every object responds to #to_s. To avoid this, you would end up with something like: (WARNING! Fugly code ahead.)</p> <p>def task(name)<br> name = (Symbol === name ? name.to_s : name.to_str)<br> ...<br> end</p> <p>There has to be a clearer solution than that.</p> <p>P.S. Just for fun of it I tried this on rake and discovered the Jim decided not to care what get&#39;s passed to task. Try this in your Rakefile:</p> <pre>desc &quot;OMG!&quot; task Object.new do puts &quot;OMG is right!&quot; end </pre> <p>A Duck-typing true beleiver!!! Yea, looks like a bug to me. If the user really needs it they can call #to_s.<br> =end</p> Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=36326 2013-02-16T04:55:39Z Eric Hodel drbrain@segment7.net <ul></ul><p>The purpose of to_str, to_int, to_ary and to_sym are to convert string, integer, array and symbol representations to objects of that class.</p> <p>For example:</p> <p>The rope data structure (which supports insertion, deletion and random access) can be used to implement a representation of a ruby string so it would be a good candidate for to_str.</p> <p>A linked-list implementation could be a good candidate for to_ary</p> <p>A roman numeral implementation that does not descend from Numeric represents an integer and would be a good candidate for to_int</p> <p>A string can be used as an identifier (as in rake) so it has to_sym.</p> <p>A symbol, being an identifier alone is not anything like a String.</p> Ruby trunk - Feature #7849: Symbol#to_str https://bugs.ruby-lang.org/issues/7849?journal_id=37340 2013-03-07T10:05:34Z Thomas Sawyer <ul></ul><p>Symbol&#39;s not anything like a Proc either, but we have Symbol#to_proc.</p> <p>Put that in your pipe and smoke it ;-)</p>
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.