Hi,
Is there a string method that takes in a regular expression AND returns
a boolean value?
Looking at the documentation i can't find a method that meets my
requirements (very surprising).
I think the idea is that as long as matching methods return either
something or nil, you can always use them in conditionals, so they
might as well return something that might have some other use.
David
--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
This is indeed a very useful idiom.
Just for the records you are talking about Regexp#=== here, not a String method.
R.
···
On 10/24/07, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:
On Thu, Oct 25, 2007 at 03:06:36AM +0900, Parv G. wrote:
> Hi,
> Is there a string method that takes in a regular expression AND returns
> a boolean value?
>
> Looking at the documentation i can't find a method that meets my
> requirements (very surprising).
> Hi,
> Is there a string method that takes in a regular expression AND returns
> a boolean value?
>
> Looking at the documentation i can't find a method that meets my
> requirements (very surprising).
% irb
>> /foo/ === 'foo'
=> true
although
'foo' === /foo/
=> false
=== is non-symmetric.
That said, I think that there are very few cases in Ruby where you
actually need an instance of either TrueClass or FalseClass, those
being when you want to use the non-shortcut methods
> instead of ||
and
& instead of &&
or the exclusive or operator ^
In these few cases you might have to do (expr == true) & expr2
instead of just expr & expr2
this gives however warnings if exp1 is a String
Robert
···
On 10/24/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
On 10/24/07, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:
> On Thu, Oct 25, 2007 at 03:06:36AM +0900, Parv G. wrote:
> > Hi,
> > Is there a string method that takes in a regular expression AND returns
> > a boolean value?
> >
> > Looking at the documentation i can't find a method that meets my
> > requirements (very surprising).
>
> % irb
> >> /foo/ === 'foo'
> => true
although
'foo' === /foo/
=> false
=== is non-symmetric.
That said, I think that there are very few cases in Ruby where you
actually need an instance of either TrueClass or FalseClass, those
being when you want to use the non-shortcut methods
> instead of ||
and
& instead of &&
or the exclusive or operator ^
In these few cases you might have to do (expr == true) & expr2
instead of just expr & expr2