Project

General

Profile

Actions

Bug #1487

closed

String#each_char must return self

Added by marcandre (Marc-Andre Lafortune) almost 15 years ago. Updated almost 13 years ago.

Status:
Closed
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
Actions #1

Updated by matz (Yukihiro Matsumoto) almost 15 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 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

Actions #2

Updated by manveru (Michael Fellinger) almost 15 years ago

=begin
On Sat, 23 May 2009 10:20:50 +0900
Yukihiro Matsumoto 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
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

Actions #3

Updated by rue (Eero Saynatkari) almost 15 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 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

Actions #4

Updated by matz (Yukihiro Matsumoto) almost 15 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 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

Actions #5

Updated by rue (Eero Saynatkari) almost 15 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 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

Actions #6

Updated by matz (Yukihiro Matsumoto) almost 15 years ago

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

=begin
Applied in changeset r23552.
=end

Actions #7

Updated by matz (Yukihiro Matsumoto) almost 15 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 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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0