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?

“G” == George [email protected] writes:

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

1.9 has String#startwith?, String#endwith?

Guy Decoux

Hi –

On Wed, 27 Sep 2006, ts wrote:

“G” == George [email protected] writes:

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

1.9 has String#startwith?, String#endwith?

No underscores between words?

David

“d” == dblack [email protected] 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

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.

Hi –

On Wed, 27 Sep 2006, ts wrote:

“d” == dblack [email protected] 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%

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

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

Hi,

In message “Re: String starts? and ends? methods”
on Wed, 27 Sep 2006 20:53:54 +0900, [email protected] 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.

Yukihiro M. 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 M. wrote:

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

Hi,

In message “Re: String starts? and ends? methods”
on Wed, 27 Sep 2006 21:29:09 +0900, [email protected] 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.

Rimantas L. 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

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

I agree completely.

James Edward G. II

Yukihiro M. 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.

Dirk M. wrote:

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.

For the Regexp-friendly users… In my opinion, a part of Ruby’s
coolness is that you have quite a lot of ways to say similar things,
which leaves you to chose the most readable for the code at hand (and
for your own eyes).

I don’t expect having another method for String will cause a huge
performance drop ;-)… And I definitely think some code will look
easier to read with that.

Cheers !

Vince

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 –

On Wed, 27 Sep 2006, Devin M. wrote:

Yukihiro M. 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

Hi,

In message “Re: String starts? and ends? methods”
on Wed, 27 Sep 2006 22:40:29 +0900, [email protected] 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.

Yukihiro M. wrote:

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

(a newbie speaking)

but what about puralization?

we gonna have “string”.respond_to?() but “string”.starts_with?()

further: could there be need for starts_with!() and ends_with!() ? or
is it bollox?

On Wed, Sep 27, 2006 at 10:25:44PM +0900, Dirk M. wrote:

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

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

That’s almost right.
You want /\Astart/ and /end\z/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs