string method that takes in a regex

Discussion in 'Ruby' started by Parv G., Oct 24, 2007.

  1. Parv G.

    Parv G. Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Parv G., Oct 24, 2007
    #1
    1. Advertising

  2. Hi --

    On Thu, 25 Oct 2007, 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).


    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!
     
    David A. Black, Oct 24, 2007
    #2
    1. Advertising

  3. On 10/24/07, 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).


    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.


    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, Oct 24, 2007
    #3
  4. Parv G.

    Parv G. Guest


    >
    > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Parv G., Oct 24, 2007
    #4
  5. Parv G.

    Randy Kramer Guest

    On Wednesday 24 October 2007 02:06 pm, Parv G. wrote:
    > 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
     
    Randy Kramer, Oct 24, 2007
    #5
  6. Parv G.

    Gary Wright Guest

    On Oct 24, 2007, at 2:06 PM, 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).


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

    Gary Wright
     
    Gary Wright, Oct 24, 2007
    #6
  7. 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
    >> /foo/ === 'bar'

    => false
    >>


    > Thanks,
    > ParvG

    --Greg
     
    Gregory Seidman, Oct 24, 2007
    #7
  8. Parv G.

    Robert Dober Guest

    On 10/24/07, Gregory Seidman <> 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
    > >> /foo/ === 'bar'

    > => false
    > >>

    This is indeed a very useful idiom.
    Just for the records you are talking about Regexp#=== here, not a String method.

    R.
    --
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
     
    Robert Dober, Oct 24, 2007
    #8
  9. On 10/24/07, Gregory Seidman <> 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

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

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, Oct 24, 2007
    #9
  10. Parv G.

    Robert Dober Guest

    On 10/24/07, Rick DeNatale <> wrote:
    > On 10/24/07, Gregory Seidman <> 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

    Hmm I prefer the !! idiom

    !! exp1 & exp2

    this gives however warnings if exp1 is a String :(
    Robert
    --
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
     
    Robert Dober, Oct 24, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cjburkha
    Replies:
    3
    Views:
    460
    cjburkha
    Mar 22, 2006
  2. Charlie@CBFC
    Replies:
    2
    Views:
    496
    Charlie@CBFC
    Sep 12, 2006
  3. joes
    Replies:
    2
    Views:
    1,065
    Daniel Pitts
    May 25, 2007
  4. Replies:
    3
    Views:
    825
    Reedick, Andrew
    Jul 1, 2008
  5. Ruby Newbee

    regex =~ string or string =~ regex?

    Ruby Newbee, Jan 4, 2010, in forum: Ruby
    Replies:
    3
    Views:
    149
    Kirk Haines
    Jan 4, 2010
Loading...

Share This Page