ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: Doing an AND in regexp char class
by David A. Black other posts by this author
May 9 2008 1:57AM messages near this date
Re: Doing an AND in regexp char class | Re: Doing an AND in regexp char class
Hi --

On Fri, 9 May 2008, 7stud -- wrote:

>  Joel VanderWerf wrote:
> > Todd Benson wrote:
> >>>>
> >>>> Thanks,
> >>> p 'foobar'.all_chars?('')
> >>>
> >>>
> >>>
> >>> cfp:~ > ruby a.rb
> >>> true
> >>> false
> >>> false
> >>
> >> Cool :)  #tr is one of those useful methods I somehow consistently forget about.
> >
> > But it can be done with regex, right? It's just more elegant with tr.
> >
> > class String
> >    def all_chars? chars
> >      if chars.empty?
> >        empty?
> >      else
> >        /\A[#{chars}]*\z/ === self
> >      end
> >    end
> > end
> >
> > p 'foobar'.all_chars?('rabof')  # => true
> > p 'foobar'.all_chars?('abc')    # => false
> > p 'foobar'.all_chars?('')       # => false
> 
>  Your method doesn't work, which can clearly be seen in these examples:
> 
>  strs = ["aaa", "bbb", "ccc"]
>  chars = "abc"
> 
>  strs.each do |str|
> 
>   if /\A[#{chars}]*/ =~ str
>     print str, " - yes"
>     puts
>   else
>     print str, " - no"
>     puts
>   end
> 
>  end
> 
>  --output:--
>  aaa - yes
>  bbb - yes
>  ccc - yes
> 
>  It should be clear from the output that even though the string "aaa"
>  passes your test, it is not true that all the characters in the string
>  "abc"  appear in in the string "aaa".

Do it the other way around (and don't forget the \z):

   if /\A[#{str}]*\z/ =~ chars

It's really the characters in str that you're testing, to make sure
that none of them fail to match the characters in chars. If the
variable names seem backwards, you can change them. It's the logic
that's important, and it works fine.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS         June 9-12            Berlin
   ADVANCING WITH RAILS   June 16-19           Berlin
   INTRO TO RAILS         June 24-27           London (Skills Matter)
See http://www.rubypal.com for details and updates!
Thread:
Todd Benson
Rick DeNatale
Ara.T.Howard
Pit Capitain
Todd Benson
Joel VanderWerf
7stud --
David A. Black
7stud --
Todd Benson
David A. Black
Joel VanderWerf
Todd Benson
David A. Black
Joel VanderWerf
David A. Black
Ara.T.Howard
Robert Dober

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved