String starts? and ends? methods

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?

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

1.9 has String#startwith?, String#endwith?

Guy Decoux

Hi --

···

On Wed, 27 Sep 2006, ts wrote:

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

1.9 has String#startwith?, String#endwith?

No underscores between words?

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

ts wrote:

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

1.9 has String#startwith?, String#endwith?

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

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

Hi --

···

On Wed, 27 Sep 2006, ts wrote:

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

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

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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 :slight_smile: 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.

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://rimantas.com/

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

Hi --

···

On Wed, 27 Sep 2006, Yukihiro Matsumoto wrote:

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 :slight_smile: 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?

Yes: add the underscores. Otherwise we'll have those exceptions "for
historical reasons" -- in this case the history of Python! :slight_smile:

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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 English-fu has me prefer starts_with? to start_with?, but general Ruby convention seems to be not to conjugate the verbs, so I'll just vote what everybody else is voting.

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! :slight_smile:

OK, done.

              matz.

I agree completely.

James Edward Gray II

···

On Sep 27, 2006, at 7:19 AM, Rimantas Liubertas 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?

                                                        matz.

+1 for changing to start_with? end_with?

Rimantas Liubertas 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?

                                                        matz.

+1 for changing to start_with? end_with?

  + 1 for me as well

  Vince

Hi --

···

On Wed, 27 Sep 2006, Devin Mullins wrote:

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 English-fu has me prefer starts_with? to start_with?, but general Ruby convention seems to be not to conjugate the verbs, so I'll just vote what everybody else is voting.

It's a little inconsistent:

   obj.respond_to? # second person
   obj.is_a? # third person

But I think second person is the most common. Or maybe are_a? just
sounded too weird :slight_smile:

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Rimantas Liubertas 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?

                                                        matz.

+1 for changing to start_with? end_with?

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

+1

fr David:
# Yes: add the underscores. Otherwise we'll have those exceptions "for
# historical reasons" -- in this case the history of Python! :slight_smile:

Hi David, nuby here. i just got hit by ::foreach. Why is it not for_each?
kind regards -botp

# David

to be honest, i don't really think this is that useful.
what wrong with

string=~/^start/
string=~/end$/

this seems to be self-explanatory enough.
greeting, Dirk.

Hi,

···

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

It's a little inconsistent:

  obj.respond_to? # second person
  obj.is_a? # third person

But I think second person is the most common. Or maybe are_a? just
sounded too weird :slight_smile:

Right. "is-a" is a too common phrase among object-oriented
programming. I wasn't brave enough to rename it "be_a" or something
like it.

              matz.

dblack@wobblini.net wrote:

It's a little inconsistent:

  obj.respond_to? # second person
  obj.is_a? # third person

But I think second person is the most common. Or maybe are_a? just
sounded too weird :slight_smile:

As the other cases are using the infinitive form,
the latter would actually be #be_a? :slight_smile:

Hal