Should substrings be modifiable in-place??
Author |
Message |
Hal E. Fulto #1 / 8
|
 Should substrings be modifiable in-place??
I've been thinking about this for a half hour or so. Rather than writing this kind of thing: str[x,y] = str[x,y].reverse str[x..z] = str[x..z].reverse I'd like to be able to do this: str[x,y].reverse! str[x..z].reverse! There is probably some good reason this won't work, though. ;) Someone care to comment? Hal
|
Mon, 01 Aug 2005 07:32:31 GMT |
|
 |
Matt Armstron #2 / 8
|
 Should substrings be modifiable in-place??
Quote: > I've been thinking about this for a half hour or so. > Rather than writing this kind of thing: > str[x,y] = str[x,y].reverse > str[x..z] = str[x..z].reverse > I'd like to be able to do this: > str[x,y].reverse! > str[x..z].reverse! > There is probably some good reason this won't work, though. ;) > Someone care to comment?
Implementation problems. The reverse! must be sent to some object -- presumably a "Substring" object that can modify the string it contains. But I'm not sure str#[] should return a non-String. I would love a Substring class, as it would make some of the things I have done in the past easier to write and more efficient. But sometimes I would want modifications to self to modify the owning string, sometimes not. -- matt
|
Mon, 01 Aug 2005 07:50:34 GMT |
|
 |
Lyle Johnso #3 / 8
|
 Should substrings be modifiable in-place??
Quote:
> I've been thinking about this for > a half hour or so. > Rather than writing this kind of thing: > str[x,y] = str[x,y].reverse > str[x..z] = str[x..z].reverse > I'd like to be able to do this: > str[x,y].reverse! > str[x..z].reverse! > There is probably some good reason this > won't work, though. ;) > Someone care to comment?
The current semantics are that this: str[x, y] = ... some string ... modifies str in place, but this: str[x, y] returns a *new* String object (and that's the clincher) which is a slice from 'str'. So if str[x, y] returns a new String, then I would *expect* that str[x, y].reverse! instantiates a new String, then calls String#reverse! to perform an in-place reverse on that new string -- but still leaves the original string (str) unmodified.
|
Mon, 01 Aug 2005 08:03:22 GMT |
|
 |
Chris Pin #4 / 8
|
 Should substrings be modifiable in-place??
Quote: ----- Original Message -----
[snip] I'd like to be able to do this: str[x,y].reverse! str[x..z].reverse! There is probably some good reason this won't work, though. ;) ----------------------------- It would require some serious syntax sugar to pull off... Why not just str.reverse!(x,y) or str.reverse!(x..y) or something? We could do this for reverse (no `!'), too, and probably lots of other String methods, to suggest 'only operate on substring'. Chris
|
Mon, 01 Aug 2005 08:13:35 GMT |
|
 |
nobu.nok.. #5 / 8
|
 Should substrings be modifiable in-place??
Hi, At Thu, 13 Feb 2003 09:13:35 +0900, Quote:
> Why not just str.reverse!(x,y) or str.reverse!(x..y) or something? We could > do this for reverse (no `!'), too, and probably lots of other String > methods, to suggest 'only operate on substring'.
Agree, and also Array#reverse. -- Nobu Nakada
|
Mon, 01 Aug 2005 08:21:25 GMT |
|
 |
Yukihiro Matsumo #6 / 8
|
 Should substrings be modifiable in-place??
Hi, In message "Should substrings be modifiable in-place??"
| str[x,y] = str[x,y].reverse | str[x..z] = str[x..z].reverse | |I'd like to be able to do this: | | str[x,y].reverse! | str[x..z].reverse! | |There is probably some good reason this |won't work, though. ;) It introduces too tight relationship between str and what str[x,y] returns. str.reverse!(x,y) is much better, although I'm not sure if it's worth implementing it to save a dozen keystrokes. How often do we need to reverse substring? matz.
|
Mon, 01 Aug 2005 08:48:34 GMT |
|
 |
Hal E. Fulto #7 / 8
|
 Should substrings be modifiable in-place??
Quote: ----- Original Message -----
Sent: Wednesday, February 12, 2003 6:48 PM Subject: Re: Should substrings be modifiable in-place?? > It introduces too tight relationship between str and what str[x,y] > returns. str.reverse!(x,y) is much better, although I'm not sure if > it's worth implementing it to save a dozen keystrokes. How often do > we need to reverse substring?
I guess the reality is: Not very often. This was more a question of "how should things work?" It has been a long time since I wrote an expression in Ruby and it didn't do what I wanted. :) I mean, I do introduce bugs, but lately they are usually a malfunction of my brain rather than a misunderstanding of Ruby. This issue is unimportant. But thanks for replying, and I see your reasoning. Hal
|
Mon, 01 Aug 2005 08:56:17 GMT |
|
 |
Martin DeMell #8 / 8
|
 Should substrings be modifiable in-place??
Quote:
> Why not just str.reverse!(x,y) or str.reverse!(x..y) or something? We could > do this for reverse (no `!'), too, and probably lots of other String > methods, to suggest 'only operate on substring'.
class String instance_methods.select {|i| i =~ /!$/}.each {|m| module_eval <<-here def s#{m}(*args) a = args.shift if (Range === a) a, b = a.first, a.last else b = args.shift end c = self[a,b] self[a,b] = c.send(:#{m},*args) end here } end a = "hello world" a.sreverse!(3,7) p a #==> "hellrow old" a.sgsub!(0,5,/l/,'p') p a #==> "hepprow old"
|
Mon, 01 Aug 2005 20:36:48 GMT |
|
|
|