You can just use the same code, but change it to:
result = str
while result[/[^<]</] line says: "While I can match a symbol, that is not
<, followed by a
<, I'll continue. Then inside the while-loop, we replace that matched thing with nothing, and ask again.
Now it works for
a<<, and will return
<. If you want it to just return an empty string, you'll need to replace the last line with: