String startswith/endswith in Ruby?

Hi all,

I’m a Python user trying to learn a bit of Ruby. I was wondering if there’s
a common way of writing “startswith” and “endswith” expressions for strings
in Ruby.

For instance:

s = ‘Hello, world!‘
s.startswith(‘Hello’) >> true
s.endswith(’!’) >> true
s.endswith(‘asdf’) >> false

I’d like to avoid having to calculate the length of the string I’m searching
for; I’d prefer not to have to write “s[0…4] == ‘Hello’” or similar.

Thanks!
Dave

···


…:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

http://extensions.rubyforge.org/rdoc/classes/String.html

then click on starts_with and ends_with and you’ll see the code.
BTW had’nt matz accepted to add these methods to the standard String
class?

···

il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin ramen@lackingtalent.com ha scritto::

Hi all,

I’m a Python user trying to learn a bit of Ruby. I was wondering if there’s
a common way of writing “startswith” and “endswith” expressions for strings
in Ruby.

Dave,

There is a common way of writing this. It uses the built-in regular
expression(s) of ruby.
Try something like.

s = “Hello, world!”
/^Hello/.match( s ) >> true
/!$/.match( s ) >> true
/asdf$/match( s ) >> false

HTH,

Zach

···

-----Original Message-----
From: Dave Benjamin [mailto:ramen@lackingtalent.com]
Sent: Tuesday, November 18, 2003 6:52 PM
To: ruby-talk ML
Subject: String startswith/endswith in Ruby?

Hi all,

I’m a Python user trying to learn a bit of Ruby. I was wondering if there’s
a common way of writing “startswith” and “endswith” expressions for strings
in Ruby.

For instance:

s = ‘Hello, world!‘
s.startswith(‘Hello’) >> true
s.endswith(’!’) >> true
s.endswith(‘asdf’) >> false

I’d like to avoid having to calculate the length of the string I’m searching
for; I’d prefer not to have to write “s[0…4] == ‘Hello’” or similar.

Thanks!
Dave


…:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

Hi David,

s = ‘Hello, world!’
s.startswith(‘Hello’) >> true
s.endswith(‘!’) >> true
s.endswith(‘asdf’) >> false

How about this …

s =~ /^Hello/ # => true
s =~ /!$/ # => true
s =~ /asdf$/ # => false

While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.

Hence, one can attack this kind of thing in a uniform manner.

Cheers,

Harry O.

Regular expressions are your friend:

s =~ /^Hello/
s =~ /!$/
s =~ /asdf$/

Regards,

Michael

···

On Wed, Nov 19, 2003 at 08:52:18AM +0900, Dave Benjamin wrote:

Hi all,

I’m a Python user trying to learn a bit of Ruby. I was wondering if there’s
a common way of writing “startswith” and “endswith” expressions for strings
in Ruby.

For instance:

s = ‘Hello, world!’
s.startswith(‘Hello’) >> true
s.endswith(‘!’) >> true
s.endswith(‘asdf’) >> false

Hi,

s = ‘Hello, world!’
s.startswith(‘Hello’) >> true
s.endswith(‘!’) >> true
s.endswith(‘asdf’) >> false

I often use

class String
def startswith(sub)
rindex(sub, 0)
end
def endswith(sub)
index(sub, -sub.length)
end
end

···

At Wed, 19 Nov 2003 08:52:18 +0900, Dave Benjamin wrote:


Nobu Nakada

I’m a Python user trying to learn a bit of Ruby. I was wondering if there’s
a common way of writing “startswith” and “endswith” expressions for strings
in Ruby.

http://extensions.rubyforge.org/rdoc/classes/String.html

then click on starts_with and ends_with and you’ll see the code.

Perfect, thanks!

BTW had’nt matz accepted to add these methods to the standard String
class?

Beats me, but it’d be a nice addition.

Thanks everyone for the quick replies. I hadn’t thought of using regexes,
though they do seem like overkill for this sort of problem. Anyway, I asked
what people commonly do, and I got my answer. =)

Peace,
Dave

···

In article 6jclrv0c35l2tcq6n968lpao29trqn0sr6@4ax.com, gabriele renzi wrote:

il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin >ramen@lackingtalent.com ha scritto::


…:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

Hi David,

While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.

well, IMO It is less error prone, may be implemented in a faster way,
and is more clear to the casual reader.

Plus, like every builtin it avoide reinventing the weel.
Did you noticed 4 people gave 3 different answers?

another quick way to do it :
if ‘ciao’[/iao$/]

:slight_smile:

···

il Wed, 19 Nov 2003 09:03:54 +0900, Harry Ohlsen harryo@qiqsolutions.com ha scritto::

gabriele renzi wrote:

While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.

well, IMO It is less error prone, may be implemented in a faster way,

True. I must say, I was surprised there weren’t built-in methods for these.

and is more clear to the casual reader.

I guess what I was getting at is that regexes are such a useful tool that I would hope most Ruby programmers would be pretty conversant with them … and the necessary regexes in this case are about as simple as you can get.

Plus, like every builtin it avoide reinventing the weel.

Of course, they’re not very big wheels :-).

Did you noticed 4 people gave 3 different answers?

Although, there were really only two, since “s =~ /^Hello/” is syntactic sugar for “/^Hello/.matches(s)”.

Ie, there were “use regexes” or “use this extension”.

another quick way to do it :
if ‘ciao’[/iao$/]

TMTOWTDI … put it down to Matz providing perl compatibility :-).

Harry Ohlsen wrote:

TMTOWTDI … put it down to Matz providing perl compatibility :-).

I just had my 21 bday. I’m putting down a few for Matz. :wink:

Zach

Zach Dennis wrote:

I just had my 21 bday. I’m putting down a few for Matz. :wink:

Congratulations! Have a couple for me!

I’ll have one for you later today; it’s bloody hot here in Sydney today, so that won’t be a problem :-).

Cheers,

Harry O.

Cheers!

···

In article AKEKIKLMCFIHPEAHKAAICEIFGOAA.zdennis@mktec.com, Zach Dennis wrote:

Harry Ohlsen wrote:

TMTOWTDI … put it down to Matz providing perl compatibility :-).

I just had my 21 bday. I’m putting down a few for Matz. :wink:


…:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :