Skip to content

Instantly share code, notes, and snippets.

@benweint
Created January 21, 2014 22:53
Show Gist options
  • Save benweint/8550146 to your computer and use it in GitHub Desktop.
Save benweint/8550146 to your computer and use it in GitHub Desktop.
String subclasses and encodings in Rubinius
#!/usr/bin/env ruby
class BadString < String; end
original = (0..255).to_a.pack("C*").force_encoding("UTF-8")
subclass = BadString.new(original)
round_tripped = subclass.to_s
puts "encoding: original = #{original.encoding}, subclass = #{subclass.encoding}, round_tripped = #{round_tripped.encoding}"
puts "valid_encoding? original = #{original.valid_encoding?}, subclass = #{subclass.valid_encoding?}, round_tripped = #{round_tripped.valid_encoding?}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment