Over a million developers have joined DZone.

Parsing UTF-8 Encoded Strings In Ruby

Instead of using $KCODE = 'UTF8' together with require 'jcode' you can use the /u regex parameter
to parse UTF-8 strings containing multibyte characters.

A Latin1 <-> UTF-8 conversion hack btw can be found here: 

For comparison just drop the u option!

string = "abc\303\244"  #  \303\244 stands for ä

puts string.scan(/./u).size

puts string.split(//u).reverse.join

puts string.gsub(/.$/u, '')

regex = Regexp.new(/..../u)
md = regex.match(string)
puts md[0].inspect


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}