From: bbiker Date: 2007-06-13T23:15:05+09:00 Subject: Re: Getting the last N bytes of a string: On Jun 12, 3:59 pm, Lloyd Linklater wrote: > > p 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.r_slice(0..9) > > What about this: > > def last_bytes(s, number_of_bytes) > if s.size > number_of_bytes then > return s.slice(s.size - number_of_bytes..s.size) > end > return s > end > > p last_bytes('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 10) > p last_bytes('WXYZ', 10) > > this is the result: > > "QRSTUVWXYZ" > "WXYZ" > > -- > Posted viahttp://www.ruby-forum.com/. Sorry to come so late in the thread. This seems to work fine for me. irb(main):001:0> str = "now is the time for all good men to come" => "now is the time for all good men to come" irb(main):002:0> str[-10..-1] => "en to come" irb(main):001:0> str = "now is the" => "now is the" irb(main):002:0> str[-10..-1] => "now is the" So I really see no need for a special method to obtain the last x bytes of a string.