How do you get the tail end of a string?

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

Aldric Giacomoni

Bertram said:
Hi,

Am Freitag, 30. Okt 2009, 22:45:10 +0900 schrieb Aldric Giacomoni:
Bertram said:
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

*shrug*
Hey, I gave you an implementation. You want it, use it. You don't want
it, make your own. Ruby's flexible. Because some behavior is not there
by default doesn't mean it's a bias.
 
A

Aldric Giacomoni

JAVoAM said:
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.
 
A

Aldric Giacomoni

Aldric said:
"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.
 
B

Bertram Scharpf

Hi Aldric,

Am Samstag, 31. Okt 2009, 01:46:10 +0900 schrieb Aldric Giacomoni:
Because some behavior is not there by default doesn't mean it's
a bias.

That's what I wanted to say: I am convinced it should be there by
default. It's just a few lines of code. It's pure logic when you
have Numeric#nonzero? and it's very useful to build default
values:

str.notempty? || "(none)" # analoigous to SQL-coalesce()

I guess it would be more useful than flip-flops or class variables.

Bertram
 
B

Bertram Scharpf

Hi,

Am Samstag, 31. Okt 2009, 01:54:10 +0900 schrieb Aldric Giacomoni:
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
 
M

Marnen Laibow-Koser

Bertram said:
Hi Aldric,

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

That's what I wanted to say: I am convinced it should be there by
default. It's just a few lines of code. It's pure logic when you
have Numeric#nonzero? and it's very useful to build default
values:

str.notempty? || "(none)" # analoigous to SQL-coalesce()

It's trivial to build:

class String
def notempty?
empty? ? nil : self
end
end
I guess it would be more useful than flip-flops or class variables.

Bertram

Best,
 
A

Aldric Giacomoni

Bertram said:
It's just a few lines of code. It's pure logic when you
have Numeric#nonzero? and it's very useful to build default
values:

str.notempty? || "(none)" # analoigous to SQL-coalesce()

I guess it would be more useful than flip-flops or class variables.

Bertram

Bertram,
So do like everyone else and extend the language to your liking ;-)
I have a small custom Rails app here which has the following in an
Integer.rb file, under /lib :

class Integer
def to_hours
self / 3600
end
end

Because I needed that (yes - just like that, no floating point division)
and it wasn't in Ruby.
 
M

Michael W. Ryder

Bertram said:
Hi,

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael said:
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.
 
R

Rajinder Yadav

Hi,

Am Freitag, 30. Okt 2009, 13:35:24 +0900 schrieb Rajinder Yadav:
Just said:
=A0 =A0 I'm actually hoping this is an embarrassing question but how do= you
get the tail end of a string? =A0All I've figured out is this:
using reg-ex

irb(main):102:0> "quick brown fox".scan /.$/
=3D> ["x"]

Why scan? There's just one match.

=A0"quick brown fox"[ /.\z/]

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



--=20
Kind Regards,
Rajinder Yadav

http://DevMentor.org

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

Bertram Scharpf

Hi,

Am Samstag, 31. Okt 2009, 04:00:09 +0900 schrieb Michael W. Ryder:
Just out of curiosity, do your methods work in a case like:
a="123456"
index = 12
puts a(-index, index)

Sure they do.

main:003> a = "123456"
=> "123456"
main:004> a.head 5
=> "12345"
main:005> a.tail 5
=> "23456"
main:006> a.head 12
=> "123456"
main:007> a.tail 8
=> "123456"

I remember that I detected this effect just when I wrote them and
that I decided against the a[-i,i] behaviour.

Bertram
 
P

Phrogz

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael said:
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.

No need for any extensions.

irb(main):001:0> "123456"[-4..-1]
=> "3456"
 
P

Phrogz

  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?

Really? You're arguing about the efficiency of something that is about
6.8µs slower?
I don't personally need to save the time. Nor do you, unless you have
a real-world program using this method that you have benchmarked and
found this particular method call to be a significant portion of your
execution time.
 
R

Rajinder Yadav

Phrogz said:
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?

Really? You're arguing about the efficiency of something that is about
6.8µs slower?

You took that personally, really?
I don't personally need to save the time. Nor do you, unless you have
a real-world program using this method that you have benchmarked and
found this particular method call to be a significant portion of your
execution time.


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

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

Phrogz

You took that personally, really?

Of course not. My point was that too often people get focused on
premature optimization. Just because one method is "four times" slower
than another does not mean that one should necessarily be chosen over
the other. My rule of thumb is:
1) Choose the right algorithms.
2) Choose the implementation most convenient for me.
3) If speed is an issue, benchmark your program and optimize the most
painful parts first.

Bertram's argument and implementation are only important, I'm saying,
to someone who has written a program that needs to process millions of
tail endings of strings in a time-critical application, and who has
proven that the speed of String#[] is a significant portion of that
time. (Important only to them, but perhaps interesting to more.)
 
J

Just Another Victim of the Ambient Morality

David A. Black said:
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.

There's nothing wrong with it other than it doesn't work. As others
have pointed out, it's:

string[-index..-1]

However, I'm more concerned with your general attitude about language
structure. Even if it did work, you can say the same thing for my original
solution:

string[index, string.size - index]

It works well... except that it uses both index _and_ string variables
twice.
Am Freitag is right. It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once. I'm very surprised that Ruby does not have this level of
expressiveness...
 
P

Phrogz

[...] As others have pointed out, it's:
string[-index..-1]
[...] It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once.  I'm very surprised that Ruby does not have this level of
expressiveness...

Is what I've done above a misquote? (Both these statements seem
attributed to you in the same message on comp.lang.ruby.)

Assuming it's not a misquote: how is the code you wrote on the second
quoted line above not exactly what you're "surprised that ruby does
not have"?
 
J

Just Another Victim of the Ambient Morality

[...] As others have pointed out, it's:
string[-index..-1]
[...] It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once. I'm very surprised that Ruby does not have this level of
expressiveness...

Is what I've done above a misquote? (Both these statements seem
attributed to you in the same message on comp.lang.ruby.)

Assuming it's not a misquote: how is the code you wrote on the second
quoted line above not exactly what you're "surprised that ruby does
not have"?




I'm sorry, my client is not quoting this post for some no good reason...

You haven't misquoted, it was simply my mistake. I was conflating two
different posts and while I was revising this one, I accidentally included
this portion which, as you've pointed out, is no longer valid...
 
D

Daniel Danopia

Hi Aldric,

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



That's what I wanted to say: I am convinced it should be there by
default. It's just a few lines of code. It's pure logic when you
have Numeric#nonzero? and it's very useful to build default
values:

  str.notempty? || "(none)"    # analoigous to SQL-coalesce()

So basically "String#notempty?" would be the same thing as
"String#any?" ?
 
M

Michael W. Ryder

Just said:
David A. Black said:
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.

There's nothing wrong with it other than it doesn't work.


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.


As others
have pointed out, it's:

string[-index..-1]

However, I'm more concerned with your general attitude about language
structure. Even if it did work, you can say the same thing for my original
solution:

string[index, string.size - index]

It works well... except that it uses both index _and_ string variables
twice.
Am Freitag is right. It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once. I'm very surprised that Ruby does not have this level of
expressiveness...
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top