From: Erik Hollensbe Date: 2007-06-12T18:15:38+09:00 Subject: Re: Getting the last N bytes of a string: On 2007-06-04 10:40:32 -0700, Ryan Davis said: > > On Jun 1, 2007, at 23:14 , Brian Candler wrote: > >> I think I'll stick with buf[-10,10] || buf. I was just wondering if I'd >> missed something obvious like buf.last(10), after staring at 'ri String' too >> long. > > you could do that too: > > >> "blah blah blah".split(//).last(4).join > => "blah" > > or do what you were looking for: > > >> class String; def last(n=1); self[-n, n] || self; end; end > => nil > >> "blah".last(10) > => "blah" > >> "blah blah".last(4) > => "blah" "blah blah blah".to_a.last(4).join as well. It seems like a lot of Array methods would be better served as equivalents from Enumerable. #last is one of these. (It won't nearly be as fast as a pure Array implementation, but it would be extremely useful.)