Project

General

Profile

Bug #1487

String#each_char must return self

Added by marcandre (Marc-Andre Lafortune) over 10 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
ruby -v:
ruby 1.9.2dev (2009-05-19 trunk 23489) [i386-darwin9.7.0]
Backport:
[ruby-core:23499]

Description

=begin
str.each_char{} currently returns a copy of str. It should return self. This also affects Ruby 1.8.7

Poor man's diff for rb_str_each_char:

 rb_encoding *enc;
  • VALUE orig = str; RETURN_ENUMERATOR(str, 0, 0); str = rb_str_new4(str); ...
  • return str;
  • return orig; =end

History

#1

Updated by matz (Yukihiro Matsumoto) about 10 years ago

=begin
Hi,

In message "Re: [ruby-core:23499] [Bug #1487] String#each_char must return self"
on Tue, 19 May 2009 14:27:54 +0900, Marc-Andre Lafortune redmine@ruby-lang.org writes:

|str.each_char{} currently returns a copy of str. It should return self. This also affects Ruby 1.8.7

Before accepting the proposal, could you explain why we should
return the original string? We haven't defined the return value of
each_char yet.

                        matz.

=end

#2

Updated by manveru (Michael Fellinger) about 10 years ago

=begin
On Sat, 23 May 2009 10:20:50 +0900
Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

In message "Re: [ruby-core:23499] [Bug #1487] String#each_char must
return self" on Tue, 19 May 2009 14:27:54 +0900, Marc-Andre Lafortune
redmine@ruby-lang.org writes:

|str.each_char{} currently returns a copy of str. It should return
self. This also affects Ruby 1.8.7

Before accepting the proposal, could you explain why we should
return the original string? We haven't defined the return value of
each_char yet.

                     matz.

string = 'foo'

string.object_id == string.each_codepoint{|x|}.object_id
# => true
string.object_id == string.each_byte{|x|}.object_id
# => true
string.object_id == string.each_line{|x|}.object_id
# => true
string.object_id == string.each_char{|x|}.object_id
# => false

I was hoping for a little bit of consistency as well.

--
^ manveru

=end

#3

Updated by rue (Eero Saynatkari) about 10 years ago

=begin
Excerpts from Michael Fellinger's message of Sat May 23 17:38:35 +0300 2009:

On Sat, 23 May 2009 10:20:50 +0900
Yukihiro Matsumoto matz@ruby-lang.org wrote:

Before accepting the proposal, could you explain why we should
return the original string? We haven't defined the return value of
each_char yet.

                        matz.

string = 'foo'

string.object_id == string.each_codepoint{|x|}.object_id

=> true

string.object_id == string.each_byte{|x|}.object_id

=> true

string.object_id == string.each_line{|x|}.object_id

=> true

string.object_id == string.each_char{|x|}.object_id

=> false

I was hoping for a little bit of consistency as well.

Also true for Array#each and so on. Whichever way it goes,
they should all be the same.

Philosophically, returning self from a non-mutating iterator
is the most straightforward thing to do and makes it possible
to avoid the cost of the dup.

--
Magic is insufficiently advanced technology.

=end

#4

Updated by matz (Yukihiro Matsumoto) about 10 years ago

=begin
Hi,

In message "Re: [ruby-core:23526] Re: [Bug #1487] String#each_char must return self"
on Sun, 24 May 2009 00:00:52 +0900, Eero Saynatkari ruby-ml@kittensoft.org writes:

|> I was hoping for a little bit of consistency as well.
|
|Also true for Array#each and so on. Whichever way it goes,
|they should all be the same.
|
|Philosophically, returning self from a non-mutating iterator
|is the most straightforward thing to do and makes it possible
|to avoid the cost of the dup.

The string may be modified in the block, so that you cannot avoid the
cost of the dup in this case. OK, consistency is the reason? What else?

                        matz.

=end

#5

Updated by rue (Eero Saynatkari) about 10 years ago

=begin
Excerpts from Yukihiro Matsumoto's message of Sat May 23 18:34:35 +0300 2009:

Hi,

In message "Re: [ruby-core:23526] Re: [Bug #1487] String#each_char must return
self"
on Sun, 24 May 2009 00:00:52 +0900, Eero Saynatkari ruby-ml@kittensoft.org writes:

|> I was hoping for a little bit of consistency as well.
|
|Also true for Array#each and so on. Whichever way it goes,
|they should all be the same.
|
|Philosophically, returning self from a non-mutating iterator
|is the most straightforward thing to do and makes it possible
|to avoid the cost of the dup.

The string may be modified in the block, so that you cannot avoid the
cost of the dup in this case. OK, consistency is the reason? What else?

I suppose this is true, although I am in the "enough String
to hang themselves" camp on the matter if someone thinks it
a good idea to muck with the object being iterated on (and
not using a mutating iterator.) The user can always do the
dup themselves. Might it warrant a mention in the method
documentation?

--
Magic is insufficiently advanced technology.

=end

#6

Updated by matz (Yukihiro Matsumoto) about 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r23552.
=end

#7

Updated by matz (Yukihiro Matsumoto) about 10 years ago

=begin
Hi,

In message "Re: [ruby-core:23528] Re: [Bug #1487] String#each_char must return self"
on Sun, 24 May 2009 00:47:19 +0900, Eero Saynatkari ruby-ml@kittensoft.org writes:

|I suppose this is true, although I am in the "enough String
|to hang themselves" camp on the matter if someone thinks it
|a good idea to muck with the object being iterated on (and
|not using a mutating iterator.) The user can always do the
|dup themselves.

I'm not sure if I understand what you mean. Do you mean "since the
users always dup themselves, internal dup is not needed"? If so, I
disagree, especially this case. Strings with Multibyte characters are
so easily hindered by modification, and it is not always easy to make
sure strings are not modified in the block. So if only single object
allocation can save me (copy-on-write is used internally, so no
contents are copied), I'd love to pay the cost.

                        matz.

=end

Also available in: Atom PDF