ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:67864] [ruby-trunk - Bug #10795] [Open] to_s returns references to self if called on string
       [not found] <redmine.issue-10795.20150128155806@ruby-lang.org>
@ 2015-01-28 15:58 ` fra.boffa
  2015-01-28 21:08 ` [ruby-core:67869] [ruby-trunk - Bug #10795] [Rejected] " ruby-core
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: fra.boffa @ 2015-01-28 15:58 UTC (permalink / raw
  To: ruby-core

Issue #10795 has been reported by Francesco Boffa.

----------------------------------------
Bug #10795: to_s returns references to self if called on string
https://bugs.ruby-lang.org/issues/10795

* Author: Francesco Boffa
* Status: Open
* Priority: Low
* Assignee: 
* ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
This is not actually a bug, but rather a strong violation of the Principle of least astonishment.

Lately I found a bug in the Rails project I work to. There was a method concatenating two float values with a comma, converting them to string like this:

~~~
def coords_to_string
  latitude.to_s << "," << longitude.to_s
end
~~~

The bug happened when we passed latitude and longitude as strings instead of floats.

Actually, `5.12345.to_s` returns a new string object and any operation made on that string wont affect the original 5.12345. Instead `"5.12345".to_s` will return the same instance of that string, so that the `<<` method will affect the original string.

So we found that our latitude variable was growing on each call and after a while it become "5.12345,13.12345,13.12345,13.12345,13.12345,13.12345"

This was probably made like this for performance reasons, however we found it a clear violation of the POLA, whereas we expected to_s to always return a new instance of that string.

Hope, this may help.
Francesco Boffa



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:67869] [ruby-trunk - Bug #10795] [Rejected] to_s returns references to self if called on string
       [not found] <redmine.issue-10795.20150128155806@ruby-lang.org>
  2015-01-28 15:58 ` [ruby-core:67864] [ruby-trunk - Bug #10795] [Open] to_s returns references to self if called on string fra.boffa
@ 2015-01-28 21:08 ` ruby-core
  2015-01-28 22:45 ` [ruby-core:67871] [ruby-trunk - Bug #10795] " sawadatsuyoshi
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: ruby-core @ 2015-01-28 21:08 UTC (permalink / raw
  To: ruby-core

Issue #10795 has been updated by Marc-Andre Lafortune.

Status changed from Open to Rejected

Remember, the principle of least astonishment applies to Matz only :-)

Changing this would, among other things, be a source of incompatibilities.

In any case, your code is better written

    def coords_to_string
      "#{latitude},#{longitude}"
    end

----------------------------------------
Bug #10795: to_s returns references to self if called on string
https://bugs.ruby-lang.org/issues/10795#change-51272

* Author: Francesco Boffa
* Status: Rejected
* Priority: Low
* Assignee: 
* ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
This is not actually a bug, but rather a strong violation of the Principle of least astonishment.

Lately I found a bug in the Rails project I work to. There was a method concatenating two float values with a comma, converting them to string like this:

~~~
def coords_to_string
  latitude.to_s << "," << longitude.to_s
end
~~~

The bug happened when we passed latitude and longitude as strings instead of floats.

Actually, `5.12345.to_s` returns a new string object and any operation made on that string wont affect the original 5.12345. Instead `"5.12345".to_s` will return the same instance of that string, so that the `<<` method will affect the original string.

So we found that our latitude variable was growing on each call and after a while it become "5.12345,13.12345,13.12345,13.12345,13.12345,13.12345"

This was probably made like this for performance reasons, however we found it a clear violation of the POLA, whereas we expected to_s to always return a new instance of that string.

Hope, this may help.
Francesco Boffa



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:67871] [ruby-trunk - Bug #10795] to_s returns references to self if called on string
       [not found] <redmine.issue-10795.20150128155806@ruby-lang.org>
  2015-01-28 15:58 ` [ruby-core:67864] [ruby-trunk - Bug #10795] [Open] to_s returns references to self if called on string fra.boffa
  2015-01-28 21:08 ` [ruby-core:67869] [ruby-trunk - Bug #10795] [Rejected] " ruby-core
@ 2015-01-28 22:45 ` sawadatsuyoshi
  2015-01-29  8:26 ` [ruby-core:67880] " fra.boffa
  2015-01-29 15:58 ` [ruby-core:67887] " ruby-core
  4 siblings, 0 replies; 5+ messages in thread
From: sawadatsuyoshi @ 2015-01-28 22:45 UTC (permalink / raw
  To: ruby-core

Issue #10795 has been updated by Tsuyoshi Sawada.


Marc-Andre Lafortune wrote:
> In any case, your code is better written
> 
>     def coords_to_string
>       "#{latitude},#{longitude}"
>     end

or

    def coords_to_string
      [latitude, longitude].join(",")
    end


----------------------------------------
Bug #10795: to_s returns references to self if called on string
https://bugs.ruby-lang.org/issues/10795#change-51273

* Author: Francesco Boffa
* Status: Rejected
* Priority: Low
* Assignee: 
* ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
This is not actually a bug, but rather a strong violation of the Principle of least astonishment.

Lately I found a bug in the Rails project I work to. There was a method concatenating two float values with a comma, converting them to string like this:

~~~
def coords_to_string
  latitude.to_s << "," << longitude.to_s
end
~~~

The bug happened when we passed latitude and longitude as strings instead of floats.

Actually, `5.12345.to_s` returns a new string object and any operation made on that string wont affect the original 5.12345. Instead `"5.12345".to_s` will return the same instance of that string, so that the `<<` method will affect the original string.

So we found that our latitude variable was growing on each call and after a while it become "5.12345,13.12345,13.12345,13.12345,13.12345,13.12345"

This was probably made like this for performance reasons, however we found it a clear violation of the POLA, whereas we expected to_s to always return a new instance of that string.

Hope, this may help.
Francesco Boffa



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:67880] [ruby-trunk - Bug #10795] to_s returns references to self if called on string
       [not found] <redmine.issue-10795.20150128155806@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-01-28 22:45 ` [ruby-core:67871] [ruby-trunk - Bug #10795] " sawadatsuyoshi
@ 2015-01-29  8:26 ` fra.boffa
  2015-01-29 15:58 ` [ruby-core:67887] " ruby-core
  4 siblings, 0 replies; 5+ messages in thread
From: fra.boffa @ 2015-01-29  8:26 UTC (permalink / raw
  To: ruby-core

Issue #10795 has been updated by Francesco Boffa.


For sure, our code was not the ideal for that task, and indeed, I already had changed it to one of your sane alternatives and the original committer has been appropriately thrown by the window :).

I insist, however, that this should at least be made clearer in the documentation of all the `to_s` methods available in the standard library.

Thanks for your attention.


----------------------------------------
Bug #10795: to_s returns references to self if called on string
https://bugs.ruby-lang.org/issues/10795#change-51282

* Author: Francesco Boffa
* Status: Rejected
* Priority: Low
* Assignee: 
* ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
This is not actually a bug, but rather a strong violation of the Principle of least astonishment.

Lately I found a bug in the Rails project I work to. There was a method concatenating two float values with a comma, converting them to string like this:

~~~
def coords_to_string
  latitude.to_s << "," << longitude.to_s
end
~~~

The bug happened when we passed latitude and longitude as strings instead of floats.

Actually, `5.12345.to_s` returns a new string object and any operation made on that string wont affect the original 5.12345. Instead `"5.12345".to_s` will return the same instance of that string, so that the `<<` method will affect the original string.

So we found that our latitude variable was growing on each call and after a while it become "5.12345,13.12345,13.12345,13.12345,13.12345,13.12345"

This was probably made like this for performance reasons, however we found it a clear violation of the POLA, whereas we expected to_s to always return a new instance of that string.

Hope, this may help.
Francesco Boffa



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:67887] [ruby-trunk - Bug #10795] to_s returns references to self if called on string
       [not found] <redmine.issue-10795.20150128155806@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-01-29  8:26 ` [ruby-core:67880] " fra.boffa
@ 2015-01-29 15:58 ` ruby-core
  4 siblings, 0 replies; 5+ messages in thread
From: ruby-core @ 2015-01-29 15:58 UTC (permalink / raw
  To: ruby-core

Issue #10795 has been updated by Marc-Andre Lafortune.


Francesco Boffa wrote:
> I insist, however, that this should at least be made clearer in the documentation of all the `to_s` methods available in the standard library.

Not sure what you mean by "all" `to_s` methods. Only `String#to_s` returns the receiver.

The document is quite explicit, both in the interface and the description. OTOH, it didn't mention the effect for subclasses, so I modified it to follow that of `Array#to_a`.

----------------------------------------
Bug #10795: to_s returns references to self if called on string
https://bugs.ruby-lang.org/issues/10795#change-51288

* Author: Francesco Boffa
* Status: Rejected
* Priority: Low
* Assignee: 
* ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
This is not actually a bug, but rather a strong violation of the Principle of least astonishment.

Lately I found a bug in the Rails project I work to. There was a method concatenating two float values with a comma, converting them to string like this:

~~~
def coords_to_string
  latitude.to_s << "," << longitude.to_s
end
~~~

The bug happened when we passed latitude and longitude as strings instead of floats.

Actually, `5.12345.to_s` returns a new string object and any operation made on that string wont affect the original 5.12345. Instead `"5.12345".to_s` will return the same instance of that string, so that the `<<` method will affect the original string.

So we found that our latitude variable was growing on each call and after a while it become "5.12345,13.12345,13.12345,13.12345,13.12345,13.12345"

This was probably made like this for performance reasons, however we found it a clear violation of the POLA, whereas we expected to_s to always return a new instance of that string.

Hope, this may help.
Francesco Boffa



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-01-29 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-10795.20150128155806@ruby-lang.org>
2015-01-28 15:58 ` [ruby-core:67864] [ruby-trunk - Bug #10795] [Open] to_s returns references to self if called on string fra.boffa
2015-01-28 21:08 ` [ruby-core:67869] [ruby-trunk - Bug #10795] [Rejected] " ruby-core
2015-01-28 22:45 ` [ruby-core:67871] [ruby-trunk - Bug #10795] " sawadatsuyoshi
2015-01-29  8:26 ` [ruby-core:67880] " fra.boffa
2015-01-29 15:58 ` [ruby-core:67887] " ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).