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.
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.
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.
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.
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.
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.
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. =)
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?
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 :-).