Getting the last N bytes of a string:

str[-100..-1]
=> nil

Cheers,
Bob

···

On 13-Jun-07, at 10:15 AM, bbiker wrote:

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.

----
Bob Hutchison -- tumblelog at <http://www.recursive.ca/so/&gt;
Recursive Design Inc. -- weblog at <http://www.recursive.ca/

                                -- works at <http://www.recursive.ca/&gt;

The problem is when there are not 10 bytes in the string.

str = "abc"
p str[-10..-1] # nil

Harry

···

On 6/13/07, bbiker <renard@nc.rr.com> wrote:

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.

--

A Look into Japanese Ruby List in English