String slice elegant way

Hello,

suppose that:

a = 'hello there'
and I need 'llo there'

a[2,a.size] works, but I find it ugly because the given length is larger than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.

Is there any elegant way, like a[2:] in Python?

(Well, I know that I can write my own [] method for String class.)

       Mage

a[2..-1]

Paolo Capriotti

···

On 3/3/06, Mage <mage@mage.hu> wrote:

       Hello,

suppose that:

a = 'hello there'
and I need 'llo there'

a[2,a.size] works, but I find it ugly because the given length is larger
than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.

Is there any elegant way, like a[2:] in Python?

a[2,a.size] works, but I find it ugly because the given length is larger
than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.

How about the UTF-8-safe way:

a[/.{2}(.*)/u,1]

Is there any elegant way ...

Oh, sorry :slight_smile:

Paul.

···

On 03/03/06, Mage <mage@mage.hu> wrote:

p "hello there".reverse.chop.chop.reverse

lol

sorry, couldn't resist that choke...

Mage schrieb:

···

      Hello,

suppose that:

a = 'hello there'
and I need 'llo there'

a[2,a.size] works, but I find it ugly because the given length is larger than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.

Is there any elegant way, like a[2:] in Python?

(Well, I know that I can write my own method for String class.)

      Mage

Paolo Capriotti wrote:

···

On 3/3/06, Mage <mage@mage.hu> wrote:

       Hello,

suppose that:

a = 'hello there'
and I need 'llo there'

a[2,a.size] works, but I find it ugly because the given length is larger
than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.

Is there any elegant way, like a[2:] in Python?

a[2..-1]

Paolo Capriotti

And consider that it doesnt require Ruby to have any special syntax for that.

:slight_smile:

lopex

Paolo Capriotti wrote:

···

On 3/3/06, Mage <mage@mage.hu> wrote:

      Hello,

suppose that:

a = 'hello there'
and I need 'llo there'

a[2,a.size] works, but I find it ugly because the given length is larger
than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.

Is there any elegant way, like a[2:] in Python?
   
a[2..-1]

Thank you, I`ve only tried a[2,-1] before the letter. My fault.

       Mage

Mage wrote:

Thank you, I`ve only tried a[2,-1] before the letter. My fault.

By the way, ranges ending with lesser number than beginning seems nasty objects to me. Their "member?" and "each" methods are not usable. Are they good for anything beyond string slicing?

       Mage

Hi --

···

On Sat, 4 Mar 2006, Mage wrote:

Mage wrote:

Thank you, I`ve only tried a[2,-1] before the letter. My fault.

By the way, ranges ending with lesser number than beginning seems nasty objects to me. Their "member?" and "each" methods are not usable. Are they good for anything beyond string slicing?

They're not good for that either; [2,-1] isn't a range object :slight_smile:

I can't remember exactly what's been said in the past about backwards
ranges, but I know stuff has been said. There's a ton of range
discussion in the ruby-talk archives. You'll find just about every
possible like, dislike, suggestion for change, etc., represented.

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black

It also doesn't work, at least not the way the OP wanted it to. But 2..-1 does.
% ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.4.0]
% irb
irb(main):001:0> "hello world"[2, -1]
=> nil
irb(main):002:0> "hello world"[2..-1]
=> "llo world"

···

On Mar 3, 2006, at 10:24 AM, dblack@wobblini.net wrote:

[2,-1] isn't a range object :slight_smile:

Hi --

···

On Sat, 4 Mar 2006, Logan Capaldo wrote:

On Mar 3, 2006, at 10:24 AM, dblack@wobblini.net wrote:

[2,-1] isn't a range object :slight_smile:

It also doesn't work, at least not the way the OP wanted it to. But 2..-1 does.

Right -- I flipped it around. The backward ranges do indeed serve
that purpose.

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! Ruby for Rails