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 Pit Capitain other posts by this author
May 11 2008 12:19AM messages near this date
Re: Doing an AND in regexp char class | Re: Doing an AND in regexp char class
2008/5/9 ara.t.howard <ara.t.howard@[...].com> :
>  On May 8, 2008, at 3:40 PM, Todd Benson wrote:
> > Using a set perspective, I can do it like this in irb...
> >
> > s1 = "hello there"
> > s2 = "ohi"
> > (s2.unpack('c*') & s1.unpack('c*')).size == s2.size
> >
> > => false
> 
>  cfp:~ > cat a.rb
>  class String
>   def all_chars? chars
>     tr(chars, '').empty?
>   end
>  end

Using String#tr is nice, but the result is not what Todd wants:

  s1 = "hello there"
  s2 = "ohe"

  (s2.unpack('c*') & s1.unpack('c*')).size == s2.size
  =>  true

  class String
   def all_chars? chars
     tr(chars, '').empty?
   end
  end

  s1.all_chars?(s2)
  =>  false

Like in the regexp examples, you have to switch self and chars:

  class String
   def all_chars? chars
     chars.tr(self, '').empty?
   end
  end

  s1.all_chars?(s2)
  =>  true

Regards,
Pit
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
© 2004 ActiveState, a division of Sophos All rights reserved