String starts? and ends? methods

There seem to be two kinds of predicates -- those which ask about what
an object is like, and those which ask about what it is; e.g:

obj.respond_to? :m # does obj respond to :m?
obj.include? item # does obj include item?
obj.all? { cond } # do all members of obj meet cond?

obj.is_a? Class # is obj a Class?
obj.instance_of? Class # is obj an instance of Class?
obj.nil? # is obj nil?

#start_with? seems to fit into the first category, and to be consistant
with the grammar of that category:

obj.start_with? 'string' # does obj start with 'string'?

Regards,
Jordan

Dirk Meijer 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$/

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

···

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

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

Yukihiro Matsumoto wrote:

Hi,

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

(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?

···

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

Hi --

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:

I think it's the second person -- at least, I seem to remember Matz
explaining it that way. So you're asking the object: [do you]
respond_to?...

We could go old school:

   obj.respondest_to?
   obj.art_a?

:slight_smile:

David

···

On Thu, 28 Sep 2006, Hal Fulton wrote:

dblack@wobblini.net wrote:

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

^ Sorry to double-post, google mail was acting up.

What functionality are you envisioning these methods would have?

James Edward Gray II

···

On Sep 27, 2006, at 9:05 AM, Jonas Hartmann wrote:

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

Hi,

···

In message "Re: String starts? and ends? methods" on Wed, 27 Sep 2006 23:05:44 +0900, Jonas Hartmann <Mail@Jonas-Hartmann.com> writes:

but what about puralization?

As a convention, Ruby method names use plain verb with singular form,
with several exceptions, e.g. is_a? etc. So we use start_with? not
starts_with?

              matz.

dblack@wobblini.net wrote:

We could go old school:

  obj.respondest_to?
  obj.art_a?

:slight_smile:

Forsooth!

Hal

So it should be #are_a?, right?

Michael Glaesemann
grzm seespotcode net

···

On Sep 28, 2006, at 9:11 , dblack@wobblini.net wrote:

Hi --

On Thu, 28 Sep 2006, Hal Fulton wrote:

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:

I think it's the second person -- at least, I seem to remember Matz
explaining it that way. So you're asking the object: [do you]
respond_to?...

is it start_with? (underscore) or startwith? ?

···

Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

So we use start_with? not
starts_with?

--
une bévue

James Edward Gray II wrote:

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

What functionality are you envisioning these methods would have?

James Edward Gray II

maybe without parameters exchanging the first or last char with a string/char, with parameters (unsigned integer) exchanging the first x chars / last x chars

i dont know if it is that useful, it just popped to my mind seeing a method? what would method! do.

newbish regards
  Jonas

···

On Sep 27, 2006, at 9:05 AM, Jonas Hartmann wrote:

Yukihiro Matsumoto wrote:

Hi,

>but what about puralization?

As a convention, Ruby method names use plain verb with singular form,
with several exceptions, e.g. is_a? etc. So we use start_with? not
starts_with?

I think this is reasonable (or maybe I am just used to it).

Those who want an English mnemonic can mentally insert
the word "does":

     str.start_with?(foo) # Does str start with foo ?

Hal

···

In message "Re: String starts? and ends? methods" > on Wed, 27 Sep 2006 23:05:44 +0900, Jonas Hartmann <Mail@Jonas-Hartmann.com> writes:

"Starts" is not the plural form of "start", it's just a conjugation.

I start/you start/he, she, it starts/we start/you start/they start

You would never say in English "a start with b", you say "a starts with b", even when both a and b are singular. I would argue that it should be the same in Ruby, and the method name should be "starts_with?"

Pete Yandell

···

On 28/09/2006, at 12:33 AM, Yukihiro Matsumoto wrote:

In message "Re: String starts? and ends? methods" > on Wed, 27 Sep 2006 23:05:44 +0900, Jonas Hartmann <Mail@Jonas- > Hartmann.com> writes:

>but what about puralization?

As a convention, Ruby method names use plain verb with singular form,
with several exceptions, e.g. is_a? etc. So we use start_with? not
starts_with?

Nah, is_a? which derives from

is you is? or is you ain't <G>

I too find that the second, instead of third person form for
predicates to we just a tad wierd.

But it is what it is.

Or maybe that should be (as a predicate).

are you what you are?

And let me say that Matz' English is much better than my Japanese.

···

On 9/27/06, Michael Glaesemann <grzm@seespotcode.net> wrote:

On Sep 28, 2006, at 9:11 , dblack@wobblini.net wrote:

> Hi --
>
> On Thu, 28 Sep 2006, Hal Fulton wrote:
>
>> 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:
>
> I think it's the second person -- at least, I seem to remember Matz
> explaining it that way. So you're asking the object: [do you]
> respond_to?...

So it should be #are_a?, right?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Une bévue wrote:

···

Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

So we use start_with? not
starts_with?

is it start_with? (underscore) or startwith? ?

that has just been changed (see discussion last hours on this topic) from without to with (so start_with)

Hi --

···

On Thu, 28 Sep 2006, Pete Yandell wrote:

On 28/09/2006, at 12:33 AM, Yukihiro Matsumoto wrote:

In message "Re: String starts? and ends? methods" >> on Wed, 27 Sep 2006 23:05:44 +0900, Jonas Hartmann >> <Mail@Jonas-Hartmann.com> writes:

>but what about puralization?

As a convention, Ruby method names use plain verb with singular form,
with several exceptions, e.g. is_a? etc. So we use start_with? not
starts_with?

"Starts" is not the plural form of "start", it's just a conjugation.

I start/you start/he, she, it starts/we start/you start/they start

You would never say in English "a start with b", you say "a starts with b", even when both a and b are singular. I would argue that it should be the same in Ruby, and the method name should be "starts_with?"

I believe the rationale is that it's in the second person: do you
start with ... ? I realize Matz mentioned singular vs. plural, but I
think that was just shorthand for with/without an 's'. In the past,
if I remember correctly, he's talked about it as a function of person.

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

You would never say in English "a start with b", you say "a starts
with b", even when both a and b are singular. I would argue that it
should be the same in Ruby, and the method name should be "starts_with?"

Ruby: foo.start_with? 'snark'
English: does foo start with 'snark'?

I could possibly see your point for something like start_with! (should it
ever exist), but I think the question form sounds correct.

- James Moore

Or since the semantic of a method call actually corresponds to sending a message (in that case a question) to an object:

       str.start_with?(foo) # Hey, string! Yeah, you there! Do you start with foo?

:slight_smile:

···

On 28 sept. 06, at 02:04, Hal Fulton wrote:

Those who want an English mnemonic can mentally insert
the word "does":

    str.start_with?(foo) # Does str start with foo ?

--
Luc Heinrich - luc@honk-honk.com - http://www.honk-honk.com

For non-predicate methods, the second person makes sense. Take String.squeeze for example...that's really about you (the second person) squeezing the string.

For predicate methods it still seems odd. In the "starts with" case, it's not you doing the starting; it's the string (third person) doing the starting.

The said, the convention in Ruby does seem to be second person no matter what. Witness the "include?" method for example (which has always felt a little odd to me too.)

Pete Yandell

···

On 28/09/2006, at 10:42 AM, dblack@wobblini.net wrote:

You would never say in English "a start with b", you say "a starts with b", even when both a and b are singular. I would argue that it should be the same in Ruby, and the method name should be "starts_with?"

I believe the rationale is that it's in the second person: do you
start with ... ? I realize Matz mentioned singular vs. plural, but I
think that was just shorthand for with/without an 's'. In the past,
if I remember correctly, he's talked about it as a function of person.