Forum: Ruby Stripping characters off a string

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.
Christopher C. (Guest)
on 2008-10-21 19:31
(Received via mailing list)
Hi everyone,


I know this is an easy question, but I want to know the best way to do
this
(i.e. the most Rubyesque).

How do I strip the last four characters off a string of undetermined
length?
I'm sure it is a one liner and doesn't require regexp. I currently have:

irb(main):010:0> a='80/tcp'
=> "80/tcp"
irb(main):011:0> a[0,a.length-4]
=> "80"

irb(main):010:0> b='5666/tcp'
=> "5666/tcp"
irb(main):011:0> b[0,a.length-4]
=> "5666"

Which is two lines, but I'm hoping you smart guys can help me out ;) I'm
guessing "chop" would work, but I'm not sure if that's the most elegant
solution

Cheers,

Chris
Hugh S. (Guest)
on 2008-10-21 19:35
(Received via mailing list)
On Wed, 22 Oct 2008, Chris Causer wrote:

> => "80/tcp"
> irb(main):011:0> a[0,a.length-4]
> => "80"
>
How does:

irb(main):001:0> str = "80/tcp"
=> "80/tcp"
irb(main):002:0> str[0...-4]
=> "80"
irb(main):003:0>

look?
        Hugh
Evgeniy D. (Guest)
on 2008-10-21 19:41
(Received via mailing list)
Isn't it like
a = a[0..-5]
is what you are looking for?
Christopher C. (Guest)
on 2008-10-21 20:47
(Received via mailing list)
On Tue, Oct 21, 2008 at 4:39 PM, Evgeniy D.
<removed_email_address@domain.invalid>wrote:

> Isn't it like
> a = a[0..-5]
> is what you are looking for?


Brilliant. Thanks Evgeniy, thanks Hugh.
Robert H. (Guest)
on 2008-10-21 23:13
your_string = "abcdef" # => "abcdef"
your_string[-4,4] = '' # => ""
your_string            # => "ab"
Christopher C. (Guest)
on 2008-10-22 02:20
(Received via mailing list)
On Tue, Oct 21, 2008 at 8:11 PM, Marc H. <removed_email_address@domain.invalid>
wrote:

> your_string = "abcdef" # => "abcdef"
> your_string[-4,4] = '' # => ""
> your_string            # => "ab"


Does that really work Marc? I always thought that

 'string'[a,b]

was to take a substring beginning at a of length b. In your case, you'd
just
get the last 4 characters 'cdef'.
Sebastian H. (Guest)
on 2008-10-22 02:26
(Received via mailing list)
Chris Causer wrote:
> > your_string = "abcdef" # => "abcdef"
> > your_string[-4,4] = '' # => ""
> > your_string            # => "ab"
>
> Does that really work

Yes, it does. If you copy and paste that code into irb, you'll see the
exact
output you see above (minus the # plus newlines plus the irb prompt)


> I always thought that
>
>  'string'[a,b]
>
> was to take a substring beginning at a of length b.

That's right.


> In your case, you'd
> just get the last 4 characters 'cdef'.

Leaving the characters "ab", which is exactly what he showed in the code
above.
Christopher C. (Guest)
on 2008-10-22 02:40
(Received via mailing list)
On Tue, Oct 21, 2008 at 11:22 PM, Sebastian H. <
removed_email_address@domain.invalid> wrote:

>
Ah, now I see. I missed the ='' on the second line.

Thanks Marc and Sebastian, but the range method is particularly alluring
:)
This topic is locked and can not be replied to.