Skip to content

Instantly share code, notes, and snippets.

@rjsamson
Created November 3, 2012 13:49
Show Gist options
  • Save rjsamson/4007428 to your computer and use it in GitHub Desktop.
Save rjsamson/4007428 to your computer and use it in GitHub Desktop.
Illustration of Rubinius Encoding Issues with String Interpolation
str = "a utf-8 string (\u20AC) with utf-8 chars".force_encoding("utf-8")
str2 = "This string is #{str} but looks like ascii-only"
puts "str: #{str}"
puts "str.ascii_only?: #{str.ascii_only?}" # => false
puts "str2: #{str2}"
puts "str2 encoding: #{str2.encoding}" # => UTF-8
puts "str2.ascii_only?: #{str2.ascii_only?}" # => true
puts "str2.dup.ascii_only?: #{str2.dup.ascii_only?}"
puts "<< broken encoding:"
str3 = "an ascii string with "
puts "str3: #{str3}"
puts "str3 encoding: #{str3.encoding}"
puts "str3 << str2"
str3 << str2
puts "str3: #{str3}"
puts "str3 encoding: #{str3.encoding}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment