Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #1980 > unrolled thread
| Started by | Patrick Tyler <patrick.a.tyler@gmail.com> |
|---|---|
| First post | 2011-03-30 12:34 -0500 |
| Last post | 2011-03-30 16:46 -0500 |
| Articles | 13 — 5 participants |
Back to article view | Back to comp.lang.ruby
string/array slices Patrick Tyler <patrick.a.tyler@gmail.com> - 2011-03-30 12:34 -0500
Re: string/array slices 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 13:08 -0500
Re: string/array slices Gary Wright <gwtmp01@mac.com> - 2011-03-30 16:22 -0500
Re: string/array slices 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 20:03 -0500
Re: string/array slices Adam Prescott <adam@aprescott.com> - 2011-03-31 02:17 -0500
Re: string/array slices 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 13:24 -0500
Re: string/array slices Gary Wright <gwtmp01@mac.com> - 2011-03-30 15:31 -0500
Re: string/array slices 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 19:55 -0500
Re: string/array slices Patrick Tyler <patrick.a.tyler@gmail.com> - 2011-03-30 14:42 -0500
Re: string/array slices Patrick Tyler <patrick.a.tyler@gmail.com> - 2011-03-30 16:16 -0500
Re: string/array slices Ross Harvey <chaos@ross.aero> - 2011-03-30 16:37 -0500
Re: string/array slices Patrick Tyler <patrick.a.tyler@gmail.com> - 2011-03-30 19:45 -0500
Re: string/array slices Patrick Tyler <patrick.a.tyler@gmail.com> - 2011-03-30 16:46 -0500
| From | Patrick Tyler <patrick.a.tyler@gmail.com> |
|---|---|
| Date | 2011-03-30 12:34 -0500 |
| Subject | string/array slices |
| Message-ID | <303f109bd62c19b354717039ac8af1a8@ruby-forum.com> |
Hello, I know that this has been covered a bit here: http://www.ruby-forum.com/topic/186437 but I'm still not certain that I understand. s = "foo" s[3] is nil, like I would expect. s[3,0] is "", instead of nil. s[4,0] is finally nil. I don't understand how I'm indexing '3' in the context of [3,0] and getting anything but nil. Since s[3] is already nil in the first place. Same for arrays: a = [:one, :two] a[2] is nil a[2,0] is an empty array ?? a[3,0] is finally nil though. I understand that in the docs these are special cases and they're not preventing me from working or anything like that. I am just curious to understand the why/how about them working this way. Thank you! -- Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-03-30 13:08 -0500 |
| Message-ID | <d8292d5926801ffdc09b8089333410bf@ruby-forum.com> |
| In reply to | #1980 |
Patrick Tyler wrote in post #990031:
> Hello,
>
> I know that this has been covered a bit here:
> http://www.ruby-forum.com/topic/186437 but I'm still not certain that I
> understand.
>
> s = "foo"
>
> s[3] is nil, like I would expect.
>
> s[3,0] is "", instead of nil.
That behaviour is contrary to the description in the 1.9.2 docs here:
http://www.ruby-doc.org/core/classes/Array.html
which say:
Returns nil if the index (or starting index) are out of range.
Because 3 is out of range, s[3,0] should return nil according to the
docs.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Gary Wright <gwtmp01@mac.com> |
|---|---|
| Date | 2011-03-30 16:22 -0500 |
| Message-ID | <890CEBA7-22CB-4317-B277-73C4B113A28A@mac.com> |
| In reply to | #1982 |
On Mar 30, 2011, at 2:08 PM, 7stud -- wrote: > Patrick Tyler wrote in post #990031: >> Hello, >> >> I know that this has been covered a bit here: >> http://www.ruby-forum.com/topic/186437 but I'm still not certain that I >> understand. >> >> s = "foo" >> >> s[3] is nil, like I would expect. >> >> s[3,0] is "", instead of nil. > > That behaviour is contrary to the description in the 1.9.2 docs here: > > http://www.ruby-doc.org/core/classes/Array.html The docs certainly could be more clear but the actual behavior is self-consistent and useful. Note: I'm assuming 1.9.X version of String. It helps to consider the numbering in the following way: -4 -3 -2 -1 <-- numbering for single argument indexing 0 1 2 3 +---+---+---+---+ | a | b | c | d | +---+---+---+---+ 0 1 2 3 4 <-- numbering for two argument indexing or start of range -4 -3 -2 -1 The common (and understandable) mistake is too assume that the semantics of the single argument index are the same as the semantics of the *first* argument in the two argument scenario (or range). They are not the same thing in practice and the documentation doesn't reflect this. The error though is definitely in the documentation and not in the implementation: single argument: the index represents a single character position within the string. The result is either the single character string found at the index or nil because there is no character at the given index. s = "" s[0] # nil because no character at that position s = "abcd" s[0] # "a" s[-4] # "a" s[-5] # nil, no characters before the first one two integer arguments: the arguments identify a portion of the string to extract or to replace. In particular, zero-width portions of the string can also be identified so that text can be inserted before or after existing characters including at the front or end of the string. In this case, the first argument does *not* identify a character position but instead identifies the space between characters as shown in the diagram above. The second argument is the length, which can be 0. s = "abcd" # each example below assumes s is reset to "abcd" To insert text before 'a': s[0,0] = "X" # "Xabcd" To insert text after 'd': s[4,0] = "Z" # "abcdZ" To replace first two characters: s[0,2] = "AB" # "ABcd" To replace last two characters: s[-2,2] = "CD" # "abCD" To replace middle two characters: s[1..3] = "XX" # "aXXd" The behavior of a range is pretty interesting. The starting point is the same as the first argument when two arguments are provided (as described above) but the end point of the range can be the 'character position' as with single indexing or the "edge position" as with two integer arguments. The difference is determined by whether the double-dot range or triple-dot range is used: s = "abcd" s[1..1] # "b" s[1..1] = "X" # "aXcd" s[1...1] # "" s[1...1] = "X" # "aXbcd", the range specifies a zero-width portion of the string s[1..3] # "bcd" s[1..3] = "X" # "aX", positions 1, 2, and 3 are replaced. s[1...3] # "bc" s[1...3] = "X" # "aXd", positions 1, 2, but not quite 3 are replaced. If you go back through these examples and insist and using the single index semantics for the double or range indexing examples you'll just get confused. You've got to use the alternate numbering I show in the ascii diagram to model the actual behavior. Gary Wright
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-03-30 20:03 -0500 |
| Message-ID | <241c102fec133f1aa78257c55e3bc4ab@ruby-forum.com> |
| In reply to | #1993 |
Gary Wright wrote in post #990065: > > In particular, zero-width portions of the string > can also be identified so that text can be inserted before or after > existing characters including at the front or end of the string. Nice explanation. Now if ruby had a self documenting docs, like php, we could add your post to the docs, and they would be much improved. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Adam Prescott <adam@aprescott.com> |
|---|---|
| Date | 2011-03-31 02:17 -0500 |
| Message-ID | <AANLkTik3uBafhZbjAFmSw5GGtPGzYwLfT+tsv2JGyEx5@mail.gmail.com> |
| In reply to | #2000 |
[Note: parts of this message were removed to make it a legal post.] On Thu, Mar 31, 2011 at 2:03 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote: > Gary Wright wrote in post #990065: > > > > In particular, zero-width portions of the string > > can also be identified so that text can be inserted before or after > > existing characters including at the front or end of the string. > > Nice explanation. Now if ruby had a self documenting docs, like php, we > could add your post to the docs, and they would be much improved. > +1 on getting this explanation into the docs.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-03-30 13:24 -0500 |
| Message-ID | <1b3cf8793f9d35fddfbf2add90bc09b1@ruby-forum.com> |
| In reply to | #1980 |
I might add, that *feature* is clearly a mistake in the ruby language because C suffers from no such problems. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Gary Wright <gwtmp01@mac.com> |
|---|---|
| Date | 2011-03-30 15:31 -0500 |
| Message-ID | <9CB0D872-8451-4995-9870-505D5E284D2F@mac.com> |
| In reply to | #1984 |
On Mar 30, 2011, at 2:24 PM, 7stud -- wrote: > I might add, that *feature* is clearly a mistake in the ruby language > because C suffers from no such problems. It is quite clear that Ruby's string model is not at all like C's so why should a particular string feature for Ruby be judged according to C's semantics? Gary Wright
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-03-30 19:55 -0500 |
| Message-ID | <e87390cc4b4b551b5f94658ba708472b@ruby-forum.com> |
| In reply to | #1988 |
Gary Wright wrote in post #990058: > On Mar 30, 2011, at 2:24 PM, 7stud -- wrote: > >> I might add, that *feature* is clearly a mistake in the ruby language >> because C suffers from no such problems. > > > It is quite clear that Ruby's string model is not at all like C's so why > should a particular string feature for Ruby be judged according to C's > semantics? > ..because ruby is written in C?? -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Patrick Tyler <patrick.a.tyler@gmail.com> |
|---|---|
| Date | 2011-03-30 14:42 -0500 |
| Message-ID | <119744c15ebd55bb3c58839fea112f5c@ruby-forum.com> |
| In reply to | #1980 |
Yep, I see. That's nutty. I wish that ruby would stop at the null, not include it. Thanks! -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Patrick Tyler <patrick.a.tyler@gmail.com> |
|---|---|
| Date | 2011-03-30 16:16 -0500 |
| Message-ID | <a75e381569a508eb75aa7e03901eae9a@ruby-forum.com> |
| In reply to | #1980 |
Gary, Do you have a different way of explaining why ruby goes past the last possible index in the situations I asked about above? You mention that Ruby's model is not at all like C's, so maybe you can help clear this up please? Thanks to you both! -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Ross Harvey <chaos@ross.aero> |
|---|---|
| Date | 2011-03-30 16:37 -0500 |
| Message-ID | <AANLkTikc1eex7P4DKMdmj-NEcS9U3b=HnyU-F-krGvMm@mail.gmail.com> |
| In reply to | #1992 |
It's not only a good thing that Ruby works this way, it's necessary.
The s[n, 0] defines a place just before or after a character, and
often before one and after another.
So:
t = 'hi'
t[0,0] = '('
t[3,0] = ')'
t
=> "(hi)"
In your adjusted version this doesn't work. It's rather interesting
that the space between the last character and a string is not nil but
a 0-length string. This makes it possible to see beforehand if the
assignment would work. Otherwise, one would just have to wait for a
(possible) IndexError exception. It even makes sense intuitively if
you think about it a minute.
Ruby, as it happens, is designed very well.
Oh, and C doesn't really have strings. I love C but it is about the
last place I would ever look for inspiration on string handling..
On Wed, Mar 30, 2011 at 2:16 PM, Patrick Tyler
<patrick.a.tyler@gmail.com> wrote:
>
> Gary,
>
> Do you have a different way of explaining why ruby goes past the last
> possible index in the situations I asked about above? You mention that
> Ruby's model is not at all like C's, so maybe you can help clear this up
> please?
>
> Thanks to you both!
>
> --
> Posted via http://www.ruby-forum.com/.
>
[toc] | [prev] | [next] | [standalone]
| From | Patrick Tyler <patrick.a.tyler@gmail.com> |
|---|---|
| Date | 2011-03-30 19:45 -0500 |
| Message-ID | <7d11b6c724d7c7487e4965e7c59ed05d@ruby-forum.com> |
| In reply to | #1994 |
Ross Harvey wrote in post #990066: > It's not only a good thing that Ruby works this way, it's necessary. > It even makes sense intuitively if > you think about it a minute. Yes and thanks to you too. I'm onboard now and agree, it's a really neat design! -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Patrick Tyler <patrick.a.tyler@gmail.com> |
|---|---|
| Date | 2011-03-30 16:46 -0500 |
| Message-ID | <1bebe883281594a91f37a2d55d7a4263@ruby-forum.com> |
| In reply to | #1980 |
Wow Gary, I really appreciate the time you took to type that up. I have never considered thinking of it in the manner that you presented it. Thanks a lot, I now understand it completely. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web