How do you get the tail end of a string?

I’m actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I’ve figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way.  This happens to me a lot and I

really appreciate Python’s curt syntax in this case: string[index:]
What’s the Ruby way?
Thank you!

I’m actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I’ve figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way.  This happens to me a lot and I

really appreciate Python’s curt syntax in this case: string[index:]
What’s the Ruby way?

Thank you!

str = “hello”
result = str[1…-1]

p result
–output:–
“ello”

Just Another Victim of the Ambient M. wrote:

You mean like string[-1]?

7stud – wrote:

I’m actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I’ve figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way.  This happens to me a lot and I

really appreciate Python’s curt syntax in this case: string[index:]
What’s the Ruby way?

Thank you!

str = “hello”
result = str[1…-1]

p result
–output:–
“ello”

…which is very similar to python–except python doesn’t include the
last index position in the slice:

str = “hello”
result = str[1:-1]
print result

–output:–
ell

Just Another Victim of the Ambient M. wrote:

I'm actually hoping this is an embarrassing question but how do you get 

the tail end of a string? All I’ve figured out is this:

using reg-ex

irb(main):102:0> “quick brown fox”.scan /.$/
=> [“x”]

irb(main):103:0> “yo mama”.scan /.$/
=> [“a”]

See Reg-ex section here: Programming Ruby: The Pragmatic Programmer's Guide

You can convert the string into an array, and use Array.last and
Array.pop
methods to consume from the end

irb(main):090:0> letters = “quick brown fox”.scan /./
=> [“q”, “u”, “i”, “c”, “k”, " ", “b”, “r”, “o”, “w”, “n”, " ", “f”,
“o”, “x”]

irb(main):091:0> letters.last
=> “x”
irb(main):092:0> letters.pop
=> “x”

irb(main):093:0> letters.last
=> “o”
irb(main):094:0> letters.pop
=> “o”

irb(main):095:0> letters.last
=> “f”
irb(main):096:0> letters.pop
=> “f”


Kind Regards,
Rajinder Y.

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their
lives.

Hi,

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:

You mean like string[-1]?

I’m sorry, I missed the part about the index number of characters. Try
string[-index, index].

That’s really ugly. You shouldn’t have to mention `index’ twice.
I would even support a language extension to solve this problem.

For my private use I wrote a suite of methods

String#head
String#tail
String#starts_with
String#ends_with

Probably I should make an own gem of it. I’ll think about that
this weekend. For so long have a look at my personal library:

http://bertram-scharpf.homelinux.com:8808/doc_root/bs-ruby-2.7/rdoc/classes/String.html

By the way: Still I’m convinced that there should be a
String#notempty?' method corresponding to Numeric#nonzero?'.

Bertram

Michael W. Ryder wrote:

Thank you!

You mean like string[-1]?

I’m sorry, I missed the part about the index number of characters. Try
string[-index, index].

Hi,

Am Freitag, 30. Okt 2009, 13:35:24 +0900 schrieb Rajinder Y.:

Just Another Victim of the Ambient M. wrote:

I'm actually hoping this is an embarrassing question but how do you 

get the tail end of a string? All I’ve figured out is this:
using reg-ex

irb(main):102:0> “quick brown fox”.scan /.$/
=> [“x”]

Why scan? There’s just one match.

“quick brown fox”[ /.\z/]

Bertram

2009/10/30 Just Another Victim of the Ambient M. <
[email protected]>

I’m actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I’ve figured out is this:

index = 4
string[index, string.size - index]

is equivalent to

string[index…-1]

-Thomas


Thomas P.
[email protected]

Pablo
Picassohttp://www.brainyquote.com/quotes/authors/p/pablo_picasso.html

  • “Computers are useless. They can only give you answers.”

Bertram S. wrote:

For my private use I wrote a suite of methods
[snip]
String#starts_with
String#ends_with

What’s wrong with the built-in #start_with and #end_with, except the
incorrect grammar?

Hi,

Am Freitag, 30. Okt 2009, 20:53:02 +0900 schrieb Albert S.:

Bertram S. wrote:

For my private use I wrote a suite of methods
[snip]
String#starts_with
String#ends_with

What’s wrong with the built-in #start_with and #end_with, except the
incorrect grammar?

That’s Ruby 1.8.7/1.9; I wrote it years ago.

It is satisfying me and it is giving me hope that parts of my
proposal are already realized.

Bertram

Hi –

On Fri, 30 Oct 2009, Bertram S. wrote:

...but surely there's a better way.

You mean like string[-1]?

I’m sorry, I missed the part about the index number of characters. Try
string[-index, index].

That’s really ugly. You shouldn’t have to mention `index’ twice.

I don’t think there’s anything wrong with it. It works well, and
there’s nothing stylistically wrong with using a local variable twice.
If index is a method that does a (re)calculation every time, you’d
want to cache it, but that’s not the case in the example.

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

Bertram S. wrote:

By the way: Still I’m convinced that there should be a
String#notempty?' method corresponding toNumeric#nonzero?’.

class String
def notempty?
!self.empty?
end
end

Good enough?

Hi,

Am Freitag, 30. Okt 2009, 22:32:19 +0900 schrieb Aldric G.:

end
My question was not how to implement it but why not to add
it to the interpreter.

Your method is returning `true’ what Numeric#nonzero? will never
do.

Bertram

Hi,

Am Freitag, 30. Okt 2009, 21:06:11 +0900 schrieb David A. Black:

On Fri, 30 Oct 2009, Bertram S. wrote:

string[-index, index].

That’s really ugly. You shouldn’t have to mention `index’ twice.

I don’t think there’s anything wrong with it. It works well, and
there’s nothing stylistically wrong with using a local variable twice.

What when it’s a method call?

Bertram

Bertram S. wrote:

My question was not how to implement it but why not to add
it to the interpreter.

Your method is returning `true’ what Numeric#nonzero? will never
do.

Bertram

You want a method which returns “nil” if the string is empty, and the
string itself if the string is nonempty?

class String
def nonempty?
self.empty? ? nil : self
end
end

Happy? :wink:
Why wouldn’t this be implemented by default, er, I don’t know, I really
can’t think of a time when I’d need this type of behavior.

Hi –

On Fri, 30 Oct 2009, Bertram S. wrote:

there’s nothing stylistically wrong with using a local variable twice.

What when it’s a method call?

Well, that would be the case I addressed in the part of my paragraph
you deleted :slight_smile:

If index is a method that does a (re)calculation every time, you’d
want to cache it, but that’s not the case in the example.

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

Hi,

Am Samstag, 31. Okt 2009, 00:12:01 +0900 schrieb Marnen Laibow-Koser:

No need.
You seem to be quite sure what you need. Look at this:

irb(main):001:0> def timer ; start = Time.now ; yield ; Time.now -
start ; end
=> nil
irb(main):002:0> index = 2
=> 2
irb(main):003:0> timer do 100_000.times { “hello”[-index…-1] } end
=> 0.888826
irb(main):004:0> timer do 100_000.times { “hello”.tail index } end
=> 0.21357

Your calculation needs more than four times as much processor
time. Don’t you need to save time?

Bertram

Hi,

Am Freitag, 30. Okt 2009, 22:45:10 +0900 schrieb Aldric G.:

Bertram S. wrote:

My question was not how to implement it but why not to add
it to the interpreter.

[yet another implementation]

Why wouldn’t this be implemented by default, er, I don’t know, I really
can’t think of a time when I’d need this type of behavior.

That’s just about you but not about other Ruby programmers.

Bertram

Bertram S. wrote:

Hi,

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:

You mean like string[-1]?

I’m sorry, I missed the part about the index number of characters. Try
string[-index, index].

That’s really ugly. You shouldn’t have to mention `index’ twice.

And you don’t. string[-index…-1] does the trick.

I would even support a language extension to solve this problem.

No need.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]