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.
D7f35c4eeff902adf48cfb4c46d6655b?d=identicon&s=25 Mage (Guest)
on 2006-03-03 14: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
22610bf26fbc5bd949c8ba4646ceb006?d=identicon&s=25 Paolo Capriotti (Guest)
on 2006-03-03 14:41
(Received via mailing list)
On 3/3/06, Mage <mage@mage.hu> wrote:
>
> Is there any elegant way, like a[2:] in Python?

a[2..-1]

Paolo Capriotti
2abf5beb51d5d66211d525a72c5cb39d?d=identicon&s=25 Paul Battley (Guest)
on 2006-03-03 14:50
(Received via mailing list)
On 03/03/06, Mage <mage@mage.hu> 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.
B97225f66bb5caac601b12735d430a0d?d=identicon&s=25 Marcin MielżyÅ?ski (Guest)
on 2006-03-03 15:26
(Received via mailing list)
Paolo Capriotti 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 Capriotti
>
>

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

:)

lopex
D7f35c4eeff902adf48cfb4c46d6655b?d=identicon&s=25 Mage (Guest)
on 2006-03-03 16:06
(Received via mailing list)
Paolo Capriotti 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
97187d7e48d59c178c7af8eaaaa3857c?d=identicon&s=25 Gregor Kopp (Guest)
on 2006-03-03 16:15
(Received via mailing list)
p "hello there".reverse.chop.chop.reverse

lol

sorry, couldn't resist that choke...


Mage schrieb:
D7f35c4eeff902adf48cfb4c46d6655b?d=identicon&s=25 Mage (Guest)
on 2006-03-03 16: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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-03 16: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 (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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-03 21:09
(Received via mailing list)
On Mar 3, 2006, at 10:24 AM, dblack@wobblini.net 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"
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-03 21:33
(Received via mailing list)
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 :-)
>
> 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! http://www.manning.com/books/black
This topic is locked and can not be replied to.