string slice elegant way

M

Mage

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
 
P

Paolo Capriotti

Hello,

suppose that:

a =3D '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
 
M

Marcin Mielżyński

Paolo said:
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.

:)

lopex
 
M

Mage

Paolo said:
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
 
M

Mage

Mage said:
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
 
G

Gregor Kopp

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

lol

sorry, couldn't resist that choke...
 
D

dblack

Hi --

Mage said:
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 :)

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 ([email protected])
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,500
Members
48,188
Latest member
GerardRush

Latest Threads

Top