Forum: Ruby string slice elegant way

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mage (Guest)
on 2006-03-03 15:26
(Received via mailing list)
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 C. (Guest)
on 2006-03-03 15:41
(Received via mailing list)
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.
Paul B. (Guest)
on 2006-03-03 15:50
(Received via mailing list)
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 :-)

Paul.
Marcin MielżyÅ?ski (Guest)
on 2006-03-03 16:26
(Received via mailing list)
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.

:)

lopex
Mage (Guest)
on 2006-03-03 17:06
(Received via mailing list)
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
Gregor K. (Guest)
on 2006-03-03 17:15
(Received via mailing list)
p "hello there".reverse.chop.chop.reverse

lol

sorry, couldn't resist that choke...


Mage schrieb:
Mage (Guest)
on 2006-03-03 17:15
(Received via mailing list)
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
unknown (Guest)
on 2006-03-03 17:24
(Received via mailing list)
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 :-)

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
Logan C. (Guest)
on 2006-03-03 22:09
(Received via mailing list)
On Mar 3, 2006, at 10:24 AM, removed_email_address@domain.invalid wrote:

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

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"
unknown (Guest)
on 2006-03-03 22:33
(Received via mailing list)
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 :-)
>
> 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
This topic is locked and can not be replied to.