Main Page | Report this Page
 
Computers Forum Index  »  Computer Languages (Ruby)  »  How do you get the tail end of a string?...
Page 2 of 2    Goto page Previous  1, 2

How do you get the tail end of a string?...

Author Message
Just Another Victim of the Ambient Morality...
Posted: Mon Nov 02, 2009 6:17 am
Guest
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:d3oHm.2522$1R3.528 at (no spam) newsfe18.iad...
Quote:

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...
 
Marnen Laibow-Koser...
Posted: Mon Nov 02, 2009 10:49 pm
Guest
G_ F_ wrote:
Quote:
On Oct 30, 4:11�am, Bertram Scharpf <li... at (no spam) bertram-scharpf.de> wrote:
[...]
By the way: Still I'm convinced that there should be a
`String#notempty?' method corresponding to `Numeric#nonzero?'.

Bertram

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

Maybe I'm totally missing the point here,

You are.

Quote:
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,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen at (no spam) marnen.org


--
Posted via http://www.ruby-forum.com/.
 
Michael W. Ryder...
Posted: Tue Nov 03, 2009 12:41 am
Guest
Just Another Victim of the Ambient Morality wrote:
Quote:
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:d3oHm.2522$1R3.528 at (no spam) newsfe18.iad...
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].
 
gf...
Posted: Tue Nov 03, 2009 3:35 am
Guest
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.

On Nov 2, 10:49 am, Marnen Laibow-Koser <mar... at (no spam) marnen.org> wrote:
Quote:
G_ F_ wrote:
On Oct 30, 4:11 am, Bertram Scharpf <li... at (no spam) bertram-scharpf.de> wrote:
[...]
By the way: Still I'm convinced that there should be a
`String#notempty?' method corresponding to `Numeric#nonzero?'.

Bertram

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

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,
--
Marnen Laibow-Koserhttp://www.marnen.org
mar... at (no spam) marnen.org

--
Posted viahttp://www.ruby-forum.com/.
 
Just Another Victim of the Ambient Morality...
Posted: Tue Nov 03, 2009 4:30 am
Guest
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:FBGHm.79$sY5.70 at (no spam) newsfe17.iad...
Quote:
Just Another Victim of the Ambient Morality wrote:
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:d3oHm.2522$1R3.528 at (no spam) newsfe18.iad...
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...
 
Just Another Victim of the Ambient Morality...
Posted: Tue Nov 03, 2009 4:48 am
Guest
"Robert Klemme" <shortcutter at (no spam) googlemail.com> wrote in message
news:2921fe3d-ba17-4cef-8e17-aed40ce9e253 at (no spam) m16g2000yqc.googlegroups.com...
On 2 Nov., 03:16, "Just Another Victim of the Ambient Morality"
<ihates... at (no spam) hotmail.com> wrote:
Quote:
"Michael W. Ryder" <_mwryde... at (no spam) gmail.com> wrote in
messagenews:d3oHm.2522$1R3.528 at (no spam) newsfe18.iad...



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?
 
Michael W. Ryder...
Posted: Tue Nov 03, 2009 5:23 am
Guest
Just Another Victim of the Ambient Morality wrote:
Quote:
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:FBGHm.79$sY5.70 at (no spam) newsfe17.iad...
Just Another Victim of the Ambient Morality wrote:
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:d3oHm.2522$1R3.528 at (no spam) newsfe18.iad...
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.
 
Just Another Victim of the Ambient Morality...
Posted: Tue Nov 03, 2009 6:17 am
Guest
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:eKKHm.15$b_.6 at (no spam) newsfe13.iad...
Quote:
Just Another Victim of the Ambient Morality wrote:
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:FBGHm.79$sY5.70 at (no spam) newsfe17.iad...
Just Another Victim of the Ambient Morality wrote:
"Michael W. Ryder" <_mwryder55 at (no spam) gmail.com> wrote in message
news:d3oHm.2522$1R3.528 at (no spam) newsfe18.iad...
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...
 
 
Page 2 of 2    Goto page Previous  1, 2
All times are GMT
The time now is Sun Nov 22, 2009 2:53 am