Skip to content

Instantly share code, notes, and snippets.

@annaswims
Created January 17, 2014 21:26
Show Gist options
  • Save annaswims/8481896 to your computer and use it in GitHub Desktop.
Save annaswims/8481896 to your computer and use it in GitHub Desktop.
railties/lib/rails.rb specifies default internal and encodings of UTF_8. If that ever gets required before a test using a pipe runs writing the data to the pipe will fail unless we set the read and write of the pipe to binmode.
def pipetest(set_binmode = false)
rd, wr = IO.pipe
if set_binmode
rd.binmode
wr.binmode
end
puts "rd.internal_encoding:#{rd.internal_encoding}"
puts "rd.external_encoding:#{rd.external_encoding}"
puts "wr.internal_encoding:#{wr.internal_encoding}"
puts "wr.external_encoding:#{wr.external_encoding}"
pid = fork {
rd.close
wr.write Marshal.dump 12356234
wr.close
exit!
}
wr.close
Process.waitpid pid
puts object_id.inspect
puts Marshal.load(rd.read).inspect
rd.close
end
puts "==binmode=true=="
pipetest(true)
puts "==binmode=false=="
pipetest
require 'rails'
puts "===After Rails Required==="
puts "==binmode=true=="
pipetest(true)
puts "==binmode=false=="
pipetest
===============the output===================
==binmode=true==
rd.internal_encoding:
rd.external_encoding:ASCII-8BIT
wr.internal_encoding:
wr.external_encoding:ASCII-8BIT
70277031773820
12356234
==binmode=false==
rd.internal_encoding:
rd.external_encoding:UTF-8
wr.internal_encoding:
wr.external_encoding:
70277031773820
12356234
===After Rails Required===
==binmode=true==
rd.internal_encoding:
rd.external_encoding:ASCII-8BIT
wr.internal_encoding:
wr.external_encoding:ASCII-8BIT
70277031773820
12356234
==binmode=false==
rd.internal_encoding:
rd.external_encoding:UTF-8
wr.internal_encoding:
wr.external_encoding:UTF-8
/Users/acarey/Desktop/pipetest.rb:14:in `write': "\x8A" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
from /Users/acarey/Desktop/pipetest.rb:14:in `block in pipetest'
from /Users/acarey/Desktop/pipetest.rb:12:in `fork'
from /Users/acarey/Desktop/pipetest.rb:12:in `pipetest'
from /Users/acarey/Desktop/pipetest.rb:38:in `<main>'
70277031773820
/Users/acarey/Desktop/pipetest.rb:23:in `load': marshal data too short (ArgumentError)
from /Users/acarey/Desktop/pipetest.rb:23:in `pipetest'
from /Users/acarey/Desktop/pipetest.rb:38:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment