Bug #1750
closedInconsistent Behavior on 1.8 and 1.9 of String#[]= with Single Fixnum Argument
Description
=begin
Brian Ford posted the following to [ruby-core:23706], but it seems to have been passed over, so I'm submitting it as a ticket. Hopefully we can clarify this behaviour, then get the affected RubySpec tests fixed. :-)
String[]= behaves differently on 1.8 and 1.9 when given a single
Fixnum argument == to the size of the string. Given the following
code:
$ cat string.rb
s = ""
p s[0]
s[0,0] = "a"
p s
s = ""
s[0] = "a"
p s
s = "x"
s[1] = "a"
p s
these are the results:
$ ruby -vw string.rb
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
nil
"a"
string.rb:8:in `[]=': index 0 out of string (IndexError)
from string.rb:8
$ ruby1.8 -vw string.rb
ruby 1.8.8dev (2009-06-04 revision 23638) [i386-darwin9.7.0]
nil
"a"
string.rb:8:in `[]=': index 0 out of string (IndexError)
from string.rb:8
$ ruby1.9 -vw string.rb
ruby 1.9.2dev (2009-05-28 trunk 23601) [i386-darwin9.7.0]
nil
"a"
"a"
"xa"
Given any string s of N characters, s[N] == nil, so I understand that
to mean that N is out of the bounds of the string. The RDoc for
String#[]= states that
"The forms that take a +Fixnum+ will raise an +IndexError+ if the value
is out of range; the +Range+ form will raise a +RangeError+, and the
+Regexp+ and +String+ forms will silently ignore the assignment."
The RDoc does not appear to have changed on 1.9.
So, my questions are:
- should ""[0] = "a" work on 1.8 the way it does on 1.9?
OR - should ""[0] = "a" raise an IndexError on 1.9?
If the answer to the above is "it should raise an IndexError on 1.9",
then should ""[0,N] = "a" also raise an IndexError?
If the answer is ""[0] = "a" should work as it does on 1.9, can we
please update the RDoc to somehow explain that while s[N] is out of
bounds of the string, you can "replace" that non-existent character
with an arbitrary length string.
Cheers,
Brian
=end
Updated by yugui (Yuki Sonoda) over 15 years ago
- Assignee set to matz (Yukihiro Matsumoto)
- Target version set to 1.9.2
=begin
=end
Updated by yugui (Yuki Sonoda) over 15 years ago
- Status changed from Open to Rejected
=begin
Matz said it's intentional.
=end