[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

string slice elegant way

Mage

3/3/2006 1:26:00 PM

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




7 Answers

Paolo Capriotti

3/3/2006 1:38:00 PM

0

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


Marcin Mielzynski

3/3/2006 2:25:00 PM

0

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.

:)

lopex

Mage

3/3/2006 3:03:00 PM

0

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

3/3/2006 3:15:00 PM

0

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




Gregor Kopp

3/3/2006 3:16:00 PM

0

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

dblack

3/3/2006 3:24:00 PM

0

dblack

3/3/2006 8:29:00 PM

0