| |||||||||||||||||||||||||||||||
|
> 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
| ||||||||||||||||||||||||||||||
© 2004-2008 readlist.com