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!

Just Another Victim of the Ambient Morality 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!

You mean like string[-1]?

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"

···

--
Posted via http://www.ruby-forum.com/\.

Just Another Victim of the Ambient Morality 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 Yadav

http://DevMentor.org

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

is equivalent to

string[index..-1]

-Thomas

···

2009/10/30 Just Another Victim of the Ambient Morality < ihatespam@hotmail.com>

   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]

--
Thomas Preymesser
thopre@gmail.com

Pablo Picasso<http://www.brainyquote.com/quotes/authors/p/pablo_picasso.html&gt;
- "Computers are useless. They can only give you answers."

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

"123456".last 4

I don't even know if this was mentioned in the thread before or not, but
hey, there it is.

···

--
Posted via http://www.ruby-forum.com/\.

Hi,

a month ago, there was a discussion about what is the best way to
get a string's tail end. There were just some solutions like

  str[-i,i]
  str[-i..-1]

Those have some problems. They're not quite fast, they don't
return proper results for i>length.

Some time ago I wrote a method String#tail that has less than 20
lines of code. I decided to make an own project of it.

Step 1.0, of course, includes String#notempty?.

  http://raa.ruby-lang.org/project/step/

Bertram

···

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Michael W. Ryder wrote:

Just Another Victim of the Ambient Morality 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!

You mean like string[-1]?

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

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

···

--
Posted via http://www.ruby-forum.com/\.

Hi,

···

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

Just Another Victim of the Ambient Morality 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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Aldric Giacomoni wrote:

"123456".last 4

I don't even know if this was mentioned in the thread before or not, but
hey, there it is.

My apologies - this IRB session was a couple of days old, and I have
some Rails libraries loaded.. This is, I think, part of ActiveSupport or
ActiveRecord.

···

--
Posted via http://www.ruby-forum.com/\.

Oh, and I thought you implemented STEP in Ruby:

:wink:

Cheers

robert

···

2009/12/1 Bertram Scharpf <lists@bertram-scharpf.de>:

Hi,

a month ago, there was a discussion about what is the best way to
get a string's tail end. There were just some solutions like

str[-i,i]
str[-i..-1]

Those have some problems. They're not quite fast, they don't
return proper results for i>length.

Some time ago I wrote a method String#tail that has less than 20
lines of code. I decided to make an own project of it.

Step 1.0, of course, includes String#notempty?.

http://raa.ruby-lang.org/project/step/

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hi,

Michael W. Ryder wrote:

Just Another Victim of the Ambient Morality 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.

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

···

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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Hi,

···

Am Samstag, 31. Okt 2009, 01:54:10 +0900 schrieb Aldric Giacomoni:

Aldric Giacomoni wrote:
>
> "123456".last 4
>
> I don't even know if this was mentioned in the thread before or not, but
> hey, there it is.

My apologies - this IRB session was a couple of days old, and I have
some Rails libraries loaded.. This is, I think, part of ActiveSupport or
ActiveRecord.

This proves that there is need for it :-)))))
scnr.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Hi,

Just Another Victim of the Ambient Morality wrote:

I&#39;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/]

I like this and need to revisit a few things, thanks =)

···

On Fri, Oct 30, 2009 at 6:49 AM, Bertram Scharpf <lists@bertram-scharpf.de> wrote:

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

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

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

Bertram Scharpf 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?

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Fri, 30 Oct 2009, Bertram Scharpf wrote:

Hi,

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

Michael W. Ryder wrote:

Just Another Victim of the Ambient Morality 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.

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 Scharpf wrote:

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

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

Good enough?

···

--
Posted via http://www.ruby-forum.com/\.

Bertram Scharpf wrote:

Hi,

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,

···

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

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Bertram Scharpf wrote:

Hi,

Michael W. Ryder wrote:

Just Another Victim of the Ambient Morality 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.

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

Just out of curiosity, do your methods work in a case like:
   a="123456"
   index = 12
   puts a(-index, index)

This is the only place where my code fails as it does not check to make sure that index is not greater than a.length.

···

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

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