String starts? and ends? methods

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
 
R

Rick DeNatale

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

Not that it's going to change, and it's all water long under the bridge, but....

Those predicates are usually part of a larger expression.

We wouldn't say

Give me my nine-iron, if does my bag have my nine-iron.

In situ which of these sounds better to the English speaker?

do_something if a.start_with?('foo')
do_something if a.respond_to?:)fred)
do_something if a.include?:)joe)

or

do_something if a.starts_with?('foo')
do_something if a.responds_to?:)fred)
do_something if a.includes?:)joe)

Try it with other statements and modifiers like unless, while, until etc.
 
R

Rick DeNatale

Marcelo Alvim wrote:

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

Well, according to
http://en.wikipedia.org/wiki/Infinitive#Uses_of_the_bare_infinitive
most modern grammarians don't consider the imperative and present
subjunctive to be uses of the bare infinitive.

I think that the older view that they are the same might actually an
example of the trap of thinking that English grammar is the same as
or very similar to Latin grammar.

A good example of this is the old-fashioned prohibition against
splitting an infinitive. "To go boldly where no one has gone before,"
instead of "To boldly go where no one has gone before." This came
from the fact that in Latin, and some of it's descendants, the
infinitive was/a single word, so splitting an infinitive is
impossible, whereas in English the use of the full infinitive (to x)
is much more common than the bare infinitive, and splitting an
infinitive seems to be both commonly used and often sounds and flows
better. The prohibition, like not ending a sentence with a
preposition, is "something, up with which I will not put."

As an analogy to what is commonly said here:

Ruby is not Java
Python is not Ruby
Ruby is not C++
English is not Latin
Le francais, n'est pas l'anglais.

Not that this is a big thing with me, I just find it interesting.
 
C

Charles O Nutter

Not that it's going to change, and it's all water long under the bridge, but....

Those predicates are usually part of a larger expression.

We wouldn't say

Give me my nine-iron, if does my bag have my nine-iron.

...if it's true that my bag does have my nine-iron

"has", "starts" and so on eliminate the 'be' verb in favor of
complicating (by conjugating) the original verb. Having lived as an
English speaker and studied other languages that both conjugate and do
not conjugate, I have to say I strongly prefer the latter. And before
you say it, yes we have to conjugate the verb 'to be' above, but
that's all we'd ever have to conjugate.
 
R

Rick DeNatale

Aliases are commonly used in Ruby Array#collect, Array#map etc

why not having them for flected forms
String#starts_with? <=> String#start_with?
personally I like String#prefix? and String#suffix? too, shorter and very
precise.

Object#responds_to? <=> ...

I guess it will make life easier for Newbies and let everybody use it's
personal feeling for what sounds better.

That was my first thought.

On the other hand it could lead to ethnic enclaves in the ruby
community based on different dialects or accents.

CockneyRuby, SouthernDrawlRuby, LanguedocRuby, SchweizerdeutschRuby? <G>

I used to tell people I met in Zurich that I felt like I was Swiss
since my mother's parents were German, my father's parents were
Italian, et je parle un peu de francais! <G>
 
L

Luc Heinrich

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

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

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?

:)
 
G

George

Rick said:
We wouldn't say

Give me my nine-iron, if does my bag have my nine-iron.

In situ which of these sounds better to the English speaker?

do_something if a.start_with?('foo')
do_something if a.respond_to?:)fred)
do_something if a.include?:)joe)

or

do_something if a.starts_with?('foo')
do_something if a.responds_to?:)fred)
do_something if a.includes?:)joe)

This is probably a consequence of Matz being Japanese. We should be grateful that he used English at
all!
 

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