[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String starts? and ends? methods

Greg Chagnon

9/27/2006 11:20:00 AM

This comes up every now and again, and lots of frameworks implement their own versions. I'm thinking
of submitting an RCR for something functionally equivalent to the following to be incorporated into
the base library:

class String
def starts?(aString)
index(aString) == 0
end

def ends?(aString)
rindex(aString) == length - aString.length
end
end

Of course, the real implementation would have much better performance. The advantage of including
something like this is that these common functions can be made both high-performance and readable.

Comments?
47 Answers

ts

9/27/2006 11:37:00 AM

0

>>>>> "G" == George <none@none.com> writes:

G> class String
G> def starts?(aString)
G> def ends?(aString)

1.9 has String#startwith?, String#endwith?


Guy Decoux




Greg Chagnon

9/27/2006 11:42:00 AM

0

ts wrote:
> G> class String
> G> def starts?(aString)
> G> def ends?(aString)
>
> 1.9 has String#startwith?, String#endwith?

Where is this documented? I can't find it anywhere.

dblack

9/27/2006 11:44:00 AM

0

ts

9/27/2006 11:49:00 AM

0

>>>>> "d" == dblack <dblack@wobblini.net> writes:

d> No underscores between words?

moulon% grep ith\? string.c
* str.startwith?([prefix]+) => true or false
* str.endwith?([suffix]+) => true or false
rb_define_method(rb_cString, "startwith?", rb_str_startwith, -1);
rb_define_method(rb_cString, "endwith?", rb_str_endwith, -1);
moulon%


Guy Decoux

dblack

9/27/2006 11:54:00 AM

0

Yukihiro Matsumoto

9/27/2006 12:16:00 PM

0

Hi,

In message "Re: String starts? and ends? methods"
on Wed, 27 Sep 2006 20:53:54 +0900, dblack@wobblini.net writes:

|It's not that I didn't believe you :-) It just seems a bit unusual,
|in terms of the general Ruby practice.

Makes sense. It was just honoring the origin (this case Python).
Since they are young (I just added it last week), it is fairly easy to
change the names. Any opinion?

matz.

Rimantas Liubertas

9/27/2006 12:20:00 PM

0

> Makes sense. It was just honoring the origin (this case Python).
> Since they are young (I just added it last week), it is fairly easy to
> change the names. Any opinion?
>
> matz.

+1 for changing to start_with? end_with?

Consistency in naming is a big plus for ruby (and minus for PHP).


Regards,
Rimantas
--
http://rim...

Robin Stocker

9/27/2006 12:23:00 PM

0

Yukihiro Matsumoto wrote:
> Makes sense. It was just honoring the origin (this case Python).
> Since they are young (I just added it last week), it is fairly easy to
> change the names. Any opinion?

My vote is also for naming them start_with? and end_with? because of
consistency and better looks :).

Robin

dblack

9/27/2006 12:29:00 PM

0

Yukihiro Matsumoto

9/27/2006 12:57:00 PM

0

Hi,

In message "Re: String starts? and ends? methods"
on Wed, 27 Sep 2006 21:29:09 +0900, dblack@wobblini.net writes:

|> Makes sense. It was just honoring the origin (this case Python).
|> Since they are young (I just added it last week), it is fairly easy to
|> change the names. Any opinion?
|
|Yes: add the underscores. Otherwise we'll have those exceptions "for
|historical reasons" -- in this case the history of Python! :-)

OK, done.

matz.