From: "Guy N. Hurst" Date: 2000-12-13T04:31:38+09:00 Subject: [ruby-talk:7099] Re: Rubyize this method "Morris, Chris" wrote: > > I ported this from Delphi ... I'd hoped there was a built in string method > to do what this does, but haven't been able to skim it out of the book yet. > I assume it's probably staring me in the face... > > def lopOff(s, substr) > return s[(s.index(substr) + substr.length)..s.length] > end > > lopOff("test this", "tes") => "t this" > If you insist on a global method, I would do it this way: irb(main):008:0> def lopOff(s,substr) irb(main):009:1> return s.sub(/^#{substr}/,"") irb(main):010:1> end nil irb(main):011:0> s="test this" "test this" irb(main):012:0> lopOff(s,"tes") "t this" Otherwise, I would prefer to add it as a method of class String like this: irb(main):013:0> class String irb(main):014:1> def lopOff(substr) irb(main):015:2> self.sub!(/^#{substr}/,"") irb(main):016:2> end irb(main):017:1> end nil irb(main):018:0> "test this".lopOff("tes") "t this" irb(main):019:0> s.lopOff("tes") "t this" irb(main):020:0> s "t this" If you do not wish for 's' to be permanently changed, remove the '!' after 'sub' in the definition; it will then do its work on a copy of itself. Guy N. Hurst -- HurstLinks Web Development http://www.hurstlinks.com/ Norfolk, VA - (757)623-9688 PHP/MySQL - Ruby/Perl - HTML/Javascript