String slice elegant way


#1

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

#2

On 3/3/06, Mage removed_email_address@domain.invalid wrote:

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

a[2…-1]

Paolo C.


#3

On 03/03/06, Mage removed_email_address@domain.invalid wrote:

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.


#4

Paolo C. wrote:

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 C.

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

:slight_smile:

lopex


#5

Paolo C. wrote:

a[2,a.size] works, but I find it ugly because the given length is larger

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

   Mage

#6

p “hello there”.reverse.chop.chop.reverse

lol

sorry, couldn’t resist that choke…

Mage schrieb:


#7

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

#8

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 (removed_email_address@domain.invalid)
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


#9

Hi –

On Sat, 4 Mar 2006, Logan C. wrote:

On Mar 3, 2006, at 10:24 AM, removed_email_address@domain.invalid 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 (removed_email_address@domain.invalid)
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


#10

On Mar 3, 2006, at 10:24 AM, removed_email_address@domain.invalid 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.
% 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”