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
|