From: Dave Thomas Date: 2000-12-13T04:37:25+09:00 Subject: [ruby-talk:7100] Re: Rubyize this method Aleksi Niemel� writes: > Morris Chris asked: > > > I'd hoped there was a built in > > string method to do what this does > > I'm not sure there's a method for this. I guess there's not. But it's not > too hard to do by yourself, and even easy enough to write inline when > needed. But here's an example of wrapped version. > > I'd use the name you've chosen (if it's good), and use regexps: > > class String > def lopOff(substr) # actually pattern might be better name > sub!(substr, "") # and sub!(/^#{substr}/, "") better > end # implementation > end > > a = "test this" > p a #=> "test this" > a.lopOff("tes") > p a #=> "t this" Not quite the same, though. Chris' method lopOff("fred", "e") would return "d" "fred".lopOff("e") would return "frd" If the latter is acceptable, then s[substr] = "" is another idiom. Dabe