From: Nikolai Weibull Date: 2005-03-21T02:33:05+09:00 Subject: Re: RCR 296: Destructive methods return self * ES (Mar 20, 2005 18:10): > > > Assertions about how strings should or should not be used are > > > based, in part, on assumptions that should be questioned. > > > > Yes, but the whole point is that they haven't and it seems that > > people don't realize how their implementation limits their use. A > > String is simply not well-suited for sequences longer than some > > certain limit (depending on system and so on). There are ways to > > alleviate these issues. For me, the most obviously good solution > > would be to add another sequence data structure that would fit > > use-cases where long sequences are expected. > Ropes are used fairly widely and would be a good solution to this > problem. In addition, I see no reason why Strings and Ropes could not > use the same idiom as Fixnum and Bignum, where the former is > automatically converted (but the latter may also be explicitly > instantiated). That's a very good point. There is definitely some sort of correlation between the relationship between Fixnum and Bignum and that between String and Rope. > > My solution will only solve the destructive vs. non-destructive > > problem for those methods that deal with substring of the original > > string, e.g., concat, chomp, delete, gsub, lstrip, rstrip, slice, > > strip, sub. For capitalize, downcase, swapcase, tr, tr_s, and > > upcase, one would still have to create a new copy. I wonder, > > though, how often the difference between capitalize and capitalize! > > in execution speed actually makes a difference, > Interesting... I certainly see the validity of your call to optimize > the nondestructive operations; I'm sure the performance can be > reasonably close to destructive when properly designed. However, it > will still have an impact in _some_ programs even when we're dealing > with these minimal differences. > And I still assert that it's conceptually better to s.chomp! than to s > = s.chomp :) Definitely. But that's due to the fact that Strings are implemented as a sequence of bytes, where we can simply decrement RSTRING(str)->len and write a '\0' at RSTRING(str)->ptr[RSTRING(str)->len - 1]. It's interesting to note that chop! returns self, so that it may easily be chained with other methods, nikolai -- ::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka ::: ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden ::: ::: page: minimalistic.org :: fun atm: gf,lps,ruby,lisp,war3 ::: main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}