On Wed, Nov 3, 2010 at 6:38 PM, James Edward Gray II
<james / graysoftinc.com> wrote:
> On Nov 3, 2010, at 11:33 AM, Ammar Ali wrote:
>> By the way, the m options seems superfluous in James' version. I get
>> the same results without it.
>
> It's not:
>
>>> "\n".sub(/.\z/u, "")
> => "\n"
>>> "\n".sub(/.\z/mu, "")
> => ""
>
> Using gsub() over sub() was a dumb mistake on my part though. sub()s all you need, since it can only match once.

Thanks for the clarification.

My method now looks like:

def chop_utf8(s)
  return unless s

  lead = s.sub(/.\z/mu, "")
  last = s.scan(/.\z/mu).first
  last = '' unless last

  [lead, last]
end

Short and sweet.

Cheers,
Ammar