How do you get the tail end of a string?

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

Michael W. Ryder said:
What method doesn't work? If you mean the string[-index, index] method it
does work fine for me. I am using 1.9.1 if that makes any difference.
The string[-index..-1] method does the same thing but I have used Business
Basic for over 20 years so my method was easier for me.

In retrospect, I'm surprised I put it so harshly but it doesn't do the
same thing as the other method. Your solution requires that you know how
long a tail you need. If, instead, you know how much of the head you need
to remove but don't know or care how long the tail is, your method is
insufficient...
 
R

Robert Klemme

What method doesn't work?  If you mean the string[-index, index] method it
does work fine for me.  I am using 1.9.1 if that makes any difference..
The string[-index..-1] method does the same thing but I have used Business
Basic for over 20 years so my method was easier for me.

    In retrospect, I'm surprised I put it so harshly but it doesn't do the
same thing as the other method.  Your solution requires that you know how
long a tail you need.  If, instead, you know how much of the head you need
to remove but don't know or care how long the tail is, your method is
insufficient...

The you can still do string[len..-1] or string.slice(len..-1) and do
not have to repeat the length. I don't really understand what all the
fuzz is about. You can get at the information you need and it's not
even difficult. It's just not that there is an explicit method which
accepts a single parameter for the length which retrieves said portion
of the beginning or end.

The reason why there is probably not a #left or #right in String is
that often string manipulation is done via regular expressions anyway
instead of via indexes. Personally I find solutions like s[/\w+\z/]
very elegant.

Kind regards

robert
 
G

gf

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

Bertram

Maybe I'm totally missing the point here, but I thought String.any?
was good enough to tell whether a string has contents:

'asdf'.any? # =3D> true
''.any? # =3D> false

'asdf'.empty? # =3D> false
''.empty? # =3D> true
 
M

Marnen Laibow-Koser

G_ F_ said:
Maybe I'm totally missing the point here,

You are.
but I thought String.any?
was good enough to tell whether a string has contents:

'asdf'.any? # => true
''.any? # => false

'asdf'.empty? # => false
''.empty? # => true

No. Bertram's idea was a bit different as far as I can tell. I think he
was saying that just as we have
5.nonzero? # => 5
we should have
'foo'.notempty? # => 'foo'

I think it's a good idea, but since it's so trivial to implement, I
doubt that it needs to be a core extension.
Best,
 
M

Michael W. Ryder

Just said:
Michael W. Ryder said:
What method doesn't work? If you mean the string[-index, index] method it
does work fine for me. I am using 1.9.1 if that makes any difference.
The string[-index..-1] method does the same thing but I have used Business
Basic for over 20 years so my method was easier for me.

In retrospect, I'm surprised I put it so harshly but it doesn't do the
same thing as the other method. Your solution requires that you know how
long a tail you need. If, instead, you know how much of the head you need
to remove but don't know or care how long the tail is, your method is
insufficient...
If all you wanted was the last character of a string then using
string[-1,1] does the same thing as string[-1..1] with less typing. This
was my original solution but seeing the index in your OP I thought you
wanted to take the last x number of characters from a string so offered
the other solution. In fact using 1.9.1 you can simplify this to just
string[-1].
 
G

gf

Good points. I got distracted by real work, then came back and had
forgotten he was looking for the value if it wasn't empty/nil.

Yes, it's easy to implement.
 
J

Just Another Victim of the Ambient Morality

Michael W. Ryder said:
Just said:
Michael W. Ryder said:
What method doesn't work? If you mean the string[-index, index] method
it does work fine for me. I am using 1.9.1 if that makes any
difference. The string[-index..-1] method does the same thing but I have
used Business Basic for over 20 years so my method was easier for me.

In retrospect, I'm surprised I put it so harshly but it doesn't do
the same thing as the other method. Your solution requires that you know
how long a tail you need. If, instead, you know how much of the head you
need to remove but don't know or care how long the tail is, your method
is insufficient...
If all you wanted was the last character of a string then using
string[-1,1] does the same thing as string[-1..1] with less typing. This
was my original solution but seeing the index in your OP I thought you
wanted to take the last x number of characters from a string so offered
the other solution. In fact using 1.9.1 you can simplify this to just
string[-1].

I saw that. I'm not sure what it was about an index that made you think
that I wanted the last x number of characters but that the second parameter
was string.size - 1 should have tipped you off as to what was known and what
was desired...
 
J

Just Another Victim of the Ambient Morality

message

What method doesn't work? If you mean the string[-index, index] method
it
does work fine for me. I am using 1.9.1 if that makes any difference.
The string[-index..-1] method does the same thing but I have used
Business
Basic for over 20 years so my method was easier for me.

In retrospect, I'm surprised I put it so harshly but it doesn't do the
same thing as the other method. Your solution requires that you know how
long a tail you need. If, instead, you know how much of the head you need
to remove but don't know or care how long the tail is, your method is
insufficient...

The you can still do string[len..-1] or string.slice(len..-1) and do
not have to repeat the length. I don't really understand what all the
fuzz is about. You can get at the information you need and it's not
even difficult. It's just not that there is an explicit method which
accepts a single parameter for the length which retrieves said portion
of the beginning or end.

The reason why there is probably not a #left or #right in String is
that often string manipulation is done via regular expressions anyway
instead of via indexes. Personally I find solutions like s[/\w+\z/]
very elegant.




What is up with my client that it randomly chooses to not quote?! I'm
sorry about this...

It's not a big fuss, it's just odd that a language that is filled with
all sorts of other nicities doesn't have a simple solution to match a simple
and common problem.
To give another example, in some sense reversed, we all appreciate how
Ruby has anonymous closures (blocks). Python has closures too, they're just
not anonymous and they are, thus, rather ugly to use. Practically, this is
meaningless since you can simply declare your closure (which is strangely
just a function) just in front of where you need it but I'm sure we all look
at that and think the same thing: it would be nice if Python had anonymous
closures like Ruby!
It's just a strange juxtaposition to see Ruby supply arrays with a .last
method but not provide strings with a simple way of getting the tail,
especially since this is present in competing languages...

Finally, I could have sworn that you were the one advocating that using
regular expressions for string manipulations that don't specifically require
their power to be unnecessarily dangerous! Am I mistaken?
 
M

Michael W. Ryder

Just said:
Michael W. Ryder said:
Just said:
What method doesn't work? If you mean the string[-index, index] method
it does work fine for me. I am using 1.9.1 if that makes any
difference. The string[-index..-1] method does the same thing but I have
used Business Basic for over 20 years so my method was easier for me.
In retrospect, I'm surprised I put it so harshly but it doesn't do
the same thing as the other method. Your solution requires that you know
how long a tail you need. If, instead, you know how much of the head you
need to remove but don't know or care how long the tail is, your method
is insufficient...
If all you wanted was the last character of a string then using
string[-1,1] does the same thing as string[-1..1] with less typing. This
was my original solution but seeing the index in your OP I thought you
wanted to take the last x number of characters from a string so offered
the other solution. In fact using 1.9.1 you can simplify this to just
string[-1].

I saw that. I'm not sure what it was about an index that made you think
that I wanted the last x number of characters but that the second parameter
was string.size - 1 should have tipped you off as to what was known and what
was desired...
I saw this in your original post, maybe it came over changed going
through the newsgroup filter:

|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]

Since index seemed to be set to an arbitrary value I thought you wanted
the last 4 characters of the string. I do something similar a lot when
parsing variables containing city, state, and zip code in one field. I
parse out the last 5 characters for the zip code and then assume that
the last two non-blank characters are the state abbreviation, and the
remainder is the city name.
Otherwise I could not understand what you were trying to do with your
code. Sorry for the confusion.
 
J

Just Another Victim of the Ambient Morality

Michael W. Ryder said:
Just said:
Michael W. Ryder said:
Just Another Victim of the Ambient Morality wrote:
What method doesn't work? If you mean the string[-index, index]
method it does work fine for me. I am using 1.9.1 if that makes any
difference. The string[-index..-1] method does the same thing but I
have used Business Basic for over 20 years so my method was easier for
me.
In retrospect, I'm surprised I put it so harshly but it doesn't do
the same thing as the other method. Your solution requires that you
know how long a tail you need. If, instead, you know how much of the
head you need to remove but don't know or care how long the tail is,
your method is insufficient...


If all you wanted was the last character of a string then using
string[-1,1] does the same thing as string[-1..1] with less typing. This
was my original solution but seeing the index in your OP I thought you
wanted to take the last x number of characters from a string so offered
the other solution. In fact using 1.9.1 you can simplify this to just
string[-1].

I saw that. I'm not sure what it was about an index that made you
think that I wanted the last x number of characters but that the second
parameter was string.size - 1 should have tipped you off as to what was
known and what was desired...
I saw this in your original post, maybe it came over changed going through
the newsgroup filter:

|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]

Since index seemed to be set to an arbitrary value I thought you wanted
the last 4 characters of the string. I do something similar a lot when
parsing variables containing city, state, and zip code in one field. I
parse out the last 5 characters for the zip code and then assume that the
last two non-blank characters are the state abbreviation, and the
remainder is the city name.
Otherwise I could not understand what you were trying to do with your
code. Sorry for the confusion.

What I most recently posted was my mistake (and too big a mistake to
call a typo). You are correct in quoting my first post.
I can see how you could interpet the original code to get the last n
characters of a string since it does, after all, return the tail end of
strings. However, the variable index can't possibly represent the length of
the tail you want to retrieve. Indeed, the first parameter of the .[]
method represents the index into the string starting from the front...
 
R

Robert Klemme

2009/11/3 Just Another Victim of the Ambient Morality <[email protected]=
m>:
message

What method doesn't work? If you mean the string[-index, index] method
it
does work fine for me. I am using 1.9.1 if that makes any difference.
The string[-index..-1] method does the same thing but I have used
Business
Basic for over 20 years so my method was easier for me.

In retrospect, I'm surprised I put it so harshly but it doesn't do the
same thing as the other method. Your solution requires that you know how
long a tail you need. If, instead, you know how much of the head you nee= d
to remove but don't know or care how long the tail is, your method is
insufficient...

The you can still do string[len..-1] or string.slice(len..-1) and do
not have to repeat the length. =A0I don't really understand what all the
fuzz is about. =A0You can get at the information you need and it's not
even difficult. =A0It's just not that there is an explicit method which
accepts a single parameter for the length which retrieves said portion
of the beginning or end.

The reason why there is probably not a #left or #right in String is
that often string manipulation is done via regular expressions anyway
instead of via indexes. =A0Personally I find solutions like s[/\w+\z/]
very elegant.
=A0 =A0What is up with my client that it randomly chooses to not quote?! = =A0I'm
sorry about this...

Maybe it's because you are using Microsoft Outlook Express... :)
=A0 =A0It's not a big fuss, it's just odd that a language that is filled = with
all sorts of other nicities doesn't have a simple solution to match a sim= ple
and common problem.

That's where I tried to argue that the problem might not be as common
as assumed. I have no overview about what all others do but - for
example - I cannot remember the last situation where I had to access
portions of a string based on indexes.
=A0 =A0To give another example, in some sense reversed, we all appreciate= how
Ruby has anonymous closures (blocks). =A0Python has closures too, they're= just
not anonymous and they are, thus, rather ugly to use. =A0Practically, thi= s is
meaningless since you can simply declare your closure (which is strangely
just a function) just in front of where you need it but I'm sure we all l= ook
at that and think the same thing: it would be nice if Python had anonymou= s
closures like Ruby!
=A0 =A0It's just a strange juxtaposition to see Ruby supply arrays with a= .last
method but not provide strings with a simple way of getting the tail,
especially since this is present in competing languages...

I see your point but I don't feel the same because I simply do not
need this feature plus I don't find the current ways to achieve it too
annoying to bother wondering why this isn't simpler.
=A0 =A0Finally, I could have sworn that you were the one advocating that = using
regular expressions for string manipulations that don't specifically requ= ire
their power to be unnecessarily dangerous! =A0Am I mistaken?

Um... I may have a blind spot there as I use regular expressions
regularly (you see, the name does make sense - at least for me :)).
At the moment I don't really see the danger there. In fact, often I
find regular expressions less dangerous because if you specify a
pattern that you want to extract your code is more robust against
changing lengths:

irb(main):007:0> %w{foo bar much_too_long}.each do |s|
irb(main):008:1* printf "%-6s %s\n", s, Time.now
irb(main):009:1> end
foo 2009-11-03 09:49:31 +0100
bar 2009-11-03 09:49:31 +0100
much_too_long 2009-11-03 09:49:31 +0100
=3D> ["foo", "bar", "much_too_long"]

Now if you are going to extract the time based on the "fact" that it
must start at column 6 of the string you're in trouble. It does work
much better by doing str[/^\S+\s+(.*)$/, 1]. This is of course just a
silly example but I hope you get the idea.

Personally I dislike using indexes for such operations for another
reason: assume you have a program that writes out columns based on
defined width (say a log file). Assume further that you have a number
of tools that need to read those log files and extract data. If you
need to stuff more into a field of the log you can easily adjust the
writing application but you have to adjust all the readers as well!
If you have defined your format in terms of patters a simple extension
of a field's length does not require any change to reading programs.

Kind regards

robert


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

Bertram Scharpf

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
 
R

Robert Klemme

2009/12/1 Bertram Scharpf said:
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

=A0str[-i,i]
=A0str[-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?.

=A0http://raa.ruby-lang.org/project/step/

Oh, and I thought you implemented STEP in Ruby:

http://de.wikipedia.org/wiki/Standard_for_the_exchange_of_product_model_dat=
a

;-)

Cheers

robert


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top