Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby 1.9 encoding: String#ljust forces US-ASCII
rbx-2.0.0-dev
Running with rubinius 2.0.0dev (1.9.3 9ad741f3 yyyy-mm-dd JI) [i686-pc-linux-gnu]
* A binary packet
ASCII-8BIT: � ("\xFF") 1 bytes
* Aligning before appending more data, with String#ljust
US-ASCII: � ("\xFF\x00\x00\x00") 4 bytes
* More data
ASCII-8BIT: � ("\xEE") 1 bytes
* Putting them together with String#+
An exception occurred running rbx-encoding-ljust.rb
undefined conversion for '"\xFF\x00\x00\x00"' from US-ASCII to ASCII-8BIT (Rubinius::EncodingClass::Encoding::CompatibilityError)
Backtrace:
Rubinius::Type.compatible_encoding at kernel/common/type19.rb:47
String#<< at kernel/common/string19.rb:420
String#+ at kernel/common/string.rb:65
Object#__script__ at rbx-encoding-ljust.rb:23
Rubinius::CodeLoader#load_script at kernel/delta/codeloader.rb:68
Rubinius::CodeLoader.load_script at kernel/delta/codeloader.rb:110
Rubinius::Loader#script at kernel/loader.rb:614
Rubinius::Loader#main at kernel/loader.rb:815
1.9.3-p194
Running with ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]
* A binary packet
ASCII-8BIT: � ("\xFF") 1 bytes
* Aligning before appending more data, with String#ljust
ASCII-8BIT: � ("\xFF\x00\x00\x00") 4 bytes
* More data
ASCII-8BIT: � ("\xEE") 1 bytes
* Putting them together with String#+
ASCII-8BIT: �� ("\xFF\x00\x00\x00\xEE") 5 bytes
jruby-1.6.7
Running with jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (Java HotSpot(TM) Client VM 1.7.0_07) [linux-i386-java]
* A binary packet
ASCII-8BIT: � ("\u00FF") 1 bytes
* Aligning before appending more data, with String#ljust
ASCII-8BIT: � ("\u00FF\u0000\u0000\u0000") 4 bytes
* More data
ASCII-8BIT: � ("\u00EE") 1 bytes
* Putting them together with String#+
ASCII-8BIT: �� ("\u00FF\u0000\u0000\u0000\u00EE") 5 bytes
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
puts "Running with #{RUBY_DESCRIPTION}"
def dump(s)
puts "#{s.encoding}: #{s} (#{s.inspect}) #{s.bytesize} bytes"
end
puts '* A binary packet'
binary = 255.chr
dump binary
puts '* Aligning before appending more data, with String#ljust'
aligned = binary.ljust(4, 0.chr)
dump aligned
puts '* More data'
more = 238.chr
dump more
puts '* Putting them together with String#+'
together = aligned + more
dump together
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.