Ask a Question related to Ruby, Design and Development.
-
Dave Benjamin #1
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) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
: 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 :
Dave Benjamin Guest
-
behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?
String#gsub(pattern) {|m| ... } It really would be nice to get match data in 'm', but this would surely break _a lot_ of scripts. How about... -
Ruby idiom for all matches in a string
What's the ruby idiom for all the matches in a string. x="ABA ABBBA CBBA" /A(B+)/.match(x) Ultimately, I want to get Thanks -- David... -
Ruby idiom for all matches in a string<Pine.LNX.4.44.0310122003300.6283-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Mon, 13 Oct 2003, Gavin Sinclair wrote: That won't do it; it only gets the first one. To get them all, you can use String#scan: ... -
String length/concat problems in Ruby 1.8.0?
Has anyone run into Ruby string length/concatenation problems in Ruby 1.8.0? One of my associates encountered difficulties when storing the... -
[ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
http://ruby-freedb.rubyforge.org/ http://ruby-serialport.rubyforge.org/ http://ruby-mp3info.rubyforge.org/ bye! --... -
gabriele renzi #2
Re: String startswith/endswith in Ruby?
il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin
<ramen@lackingtalent.com> ha scritto::
[url]http://extensions.rubyforge.org/rdoc/classes/String.html[/url]>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.
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?
gabriele renzi Guest
-
Zach Dennis #3
Re: String startswith/endswith 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) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
: 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 :
Zach Dennis Guest
-
Harry Ohlsen #4
Re: String startswith/endswith in Ruby?
Hi David,
How about this ...> s = 'Hello, world!'
> s.startswith('Hello') >> true
> s.endswith('!') >> true
> s.endswith('asdf') >> false
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.
Harry Ohlsen Guest
-
Michael Neumann #5
Re: String startswith/endswith in Ruby?
On Wed, Nov 19, 2003 at 08:52:18AM +0900, Dave Benjamin wrote:
Regular expressions are your friend:> 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
s =~ /^Hello/
s =~ /!$/
s =~ /asdf$/
Regards,
Michael
Michael Neumann Guest
-
Dave Benjamin #6
Re: String startswith/endswith in Ruby?
In article <6jclrv0c35l2tcq6n968lpao29trqn0sr6@4ax.com>, gabriele renzi wrote:
Perfect, thanks!> il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin
><ramen@lackingtalent.com> ha scritto::
>>>>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.
> [url]http://extensions.rubyforge.org/rdoc/classes/String.html[/url]
>
> then click on starts_with and ends_with and you'll see the code.
Beats me, but it'd be a nice addition.> BTW had'nt matz accepted to add these methods to the standard String
> class?
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
--
..:[ dave benjamin (ramenboy) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
: 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 :
Dave Benjamin Guest
-
gabriele renzi #7
Re: String startswith/endswith in Ruby?
il Wed, 19 Nov 2003 09:03:54 +0900, Harry Ohlsen
<harryo@qiqsolutions.com> ha scritto::
>Hi David,
>well, IMO It is less error prone, may be implemented in a faster way,>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.
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$/]
>gabriele renzi Guest
-
Harry Ohlsen #8
Re: String startswith/endswith in Ruby?
gabriele renzi wrote:
True. I must say, I was surprised there weren't built-in methods for these.>>>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,
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.> and is more clear to the casual reader.
Of course, they're not very big wheels :-).> Plus, like every builtin it avoide reinventing the weel.
Although, there were really only two, since "s =~ /^Hello/" is syntactic sugar for "/^Hello/.matches(s)".> Did you noticed 4 people gave 3 different answers?
Ie, there were "use regexes" or "use this extension".
TMTOWTDI ... put it down to Matz providing perl compatibility :-).> another quick way to do it :
> if 'ciao'[/iao$/]
Harry Ohlsen Guest
-
Zach Dennis #9
Re: String startswith/endswith in Ruby?
Harry Ohlsen wrote:
I just had my 21 bday. I'm putting down a few for Matz.>>TMTOWTDI ... put it down to Matz providing perl compatibility :-).
Zach
Zach Dennis Guest
-
Harry Ohlsen #10
Re: String startswith/endswith in Ruby?
Zach Dennis wrote:
Congratulations! Have a couple for me!> I just had my 21 bday. I'm putting down a few for Matz.
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.
Harry Ohlsen Guest
-
Dave Benjamin #11
Re: String startswith/endswith in Ruby?
In article <AKEKIKLMCFIHPEAHKAAICEIFGOAA.zdennis@mktec.com> , Zach Dennis wrote:
Cheers!> 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.
--
..:[ dave benjamin (ramenboy) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
: 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 :
Dave Benjamin Guest



Reply With Quote

