[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: String Intersection. A better way

Pit Capitain

3/28/2007 1:24:00 PM

Daniel N schrieb:
> I have an issue with strings. I want the intesection of two strings but
> I'm
> not sure how to go about it. I have two cases.
> (...)

Daniel, if your strings aren't too big, you could try this:

class String
def intersection_at_start(other)
not empty? and other[/#{split(//)*"("}#{")?"*(size-1)}\z/]
end
def intersection_at_end(other)
other.intersection_at_start(self)
end
def &(other)
intersection_at_start(other) or intersection_at_end(other) or ""
end
end

I don't know if this is faster than your original implementation, and it
certainly isn't easier to understand, but at least it passes Rob's tests.

Regards,
Pit