string method that takes in a regex

P

Parv G.

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

Thanks,
ParvG
 
D

David A. Black

Hi --

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!
 
R

Rick DeNatale

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

String#match takes a regexp and returns either nil or a MatchData.

In Ruby nil is a boolean false, and anything other than nil or false
is a boolean true, so this effectively does return a boolean value.
 
P

Parv G.

In Ruby nil is a boolean false, and anything other than nil or false
is a boolean true, so this effectively does return a boolean value.

I did not know this; this is very helpful.

Thanks David and Rick.

Parv
 
R

Randy Kramer

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

Look at the match method (or =~ )--since nil is false and anything else is
true, I would think that you could use it.

Randy Kramer
 
G

Gary Wright

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

str = "abc"
str[/bc/] # "bc" which evaluates as true
str[/bd/] # nil which evaluates as false

Gary Wright
 
R

Rick DeNatale

% irb
=> 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

There's also !!expr but be careful with that
http://www.therailsway.com/2007/8/1/dangers-of-cargo-culting
 
R

Robert Dober

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
Hmm I prefer the !! idiom

!! exp1 & exp2

this gives however warnings if exp1 is a String :(
Robert
 

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,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top