String starts? and ends? methods

Y

Yukihiro Matsumoto

Hi,

In message "Re: String starts? and ends? methods"

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

Jonas Hartmann

Une said:
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)
 
J

Jonas Hartmann

James said:
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
 
H

Hal Fulton

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

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


Hal
 
H

Hal Fulton

Yukihiro said:
Hi,

In message "Re: String starts? and ends? methods"

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

dblack

Hi --

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

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?

:)


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
P

Pete Yandell

In message "Re: String starts? and ends? methods"
|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?"

Pete Yandell
 
D

dblack

Hi --

"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 | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
S

Suraj N. Kurapati

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rimantas said:
+1 for changing to start_with? end_with?

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

+1
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFFGyk9mV9O7RYnKMcRAlwaAKCE8ydXuZC+Y+UWA0F4dbYOZV6+/gCfS9pw
Pg5WAPp0YDsnjE38Qb6akd0=
=inCH
-----END PGP SIGNATURE-----
 
J

James Moore

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
 
P

Pete Yandell

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.

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
 
M

Marcelo Alvim

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.

Sorry for entering this thread now to give my two cents. I'm sorry
because I'm a Ruby newbie AND a non-native english speaker, but I
thought I should say this.

I don't think the person is really the question here. I think it's the
imperative form of the verb.
In that case, I have always interpreted that (both in english and in
my native language) as you telling the string to squeeze itself.
File.open, for example, reads like "Hey, File class, open this for me,
please". I'm imperative, but polite :).

I think that's why most methods which do not ask for something are
written like that. I'd also prefer the "starts_with?" name, but if
"start_with?" is more consistent, I say stay with that.

Cheers,
Alvim.
 
M

Michael Glaesemann

Hi --

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

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?

Michael Glaesemann
grzm seespotcode net
 
R

Rick DeNatale

Hi --

(e-mail address removed) 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 :)

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

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?

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

Pete Yandell

Sorry for entering this thread now to give my two cents. I'm sorry
because I'm a Ruby newbie AND a non-native english speaker, but I
thought I should say this.

I don't think the person is really the question here. I think it's the
imperative form of the verb.
In that case, I have always interpreted that (both in english and in
my native language) as you telling the string to squeeze itself.
File.open, for example, reads like "Hey, File class, open this for me,
please". I'm imperative, but polite :).

I think you're absolutely right Marcelo, and you've once again proved
that non-native English speakers tend to know way more about grammar
than native English speakers like me. :)

Pete Yandell
 
P

Peña, Botp

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

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

# David
 
H

Hal Fulton

Marcelo said:
Sorry for entering this thread now to give my two cents. I'm sorry
because I'm a Ruby newbie AND a non-native english speaker, but I
thought I should say this.

I don't think the person is really the question here. I think it's the
imperative form of the verb.
In that case, I have always interpreted that (both in english and in
my native language) as you telling the string to squeeze itself.
File.open, for example, reads like "Hey, File class, open this for me,
please". I'm imperative, but polite :).

Interesting. Some methods are imperative in form. But the ones
that are interrogative take the form they do (IMO) because it's
the infinitive form of the verb. (Of course, the imperative
usually uses the infinitive...)
I think that's why most methods which do not ask for something are
written like that. I'd also prefer the "starts_with?" name, but if
"start_with?" is more consistent, I say stay with that.

We agree on that. Besides, after 13 years, this convention is not
likely to change.


Hal
 
M

MonkeeSage

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
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top