From: "Michael W. Ryder" <_mwryder55@...> Date: 2009-10-31T04:00:09+09:00 Subject: Re: How do you get the tail end of a string? Bertram Scharpf wrote: > Hi, > > Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder: >> Michael W. Ryder wrote: >>> Just Another Victim of the Ambient Morality wrote: >>>> I'm actually hoping this is an embarrassing question but how do you >>>> get the tail end of a string? All I've figured out is this: >>>> >>>> index = 4 >>>> string[index, string.size - index] >>>> >>>> ...but surely there's a better way. >>>> >>> You mean like string[-1]? >> I'm sorry, I missed the part about the index number of characters. Try >> string[-index, index]. > > That's really ugly. You shouldn't have to mention `index' twice. > I would even support a language extension to solve this problem. > > For my private use I wrote a suite of methods > > String#head > String#tail > String#starts_with > String#ends_with > Just out of curiosity, do your methods work in a case like: a="123456" index = 12 puts a(-index, index) This is the only place where my code fails as it does not check to make sure that index is not greater than a.length. > Probably I should make an own gem of it. I'll think about that > this weekend. For so long have a look at my personal library: > > http://bertram-scharpf.homelinux.com:8808/doc_root/bs-ruby-2.7/rdoc/classes/String.html > > By the way: Still I'm convinced that there should be a > `String#notempty?' method corresponding to `Numeric#nonzero?'. > > Bertram > >