You need to use ‘wb’ mode to prevent Ruby from transforming eolns.

Tomas

From: ironruby-core-boun...@rubyforge.org 
[mailto:ironruby-core-boun...@rubyforge.org] On Behalf Of Ivan Porto Carrero
Sent: Friday, October 23, 2009 5:47 AM
To: hako...@hclausen.net; ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] byte[] to ruby string

<"This is a test\n"> expected but was
<"This is a test\r\n">.

windows default is to use CRLF for a new line and the CLR inherits that
But for ruby the new line constant is a LF which ruby inherits from *nix based 
systems

the dirty workaround is

either specify data0 = "This is a test\r\n"
or
data1.gsub! /\n/, "\r\n"
or
data2.gsub! /\r\n/, "\n"

AFAIK there is no way to override the Environment.NewLine constant in a way the 
CLR will pick it up.

---
Met vriendelijke groeten - Best regards - Salutations
Ivan Porto Carrero
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)


On Fri, Oct 23, 2009 at 2:31 PM, Håkon Clausen 
<hako...@hclausen.net<mailto:hako...@hclausen.net>> wrote:
Thanks,
CreateBinary made the picture file pass the test.
I'm having some trouble with some plain text and newlines. Any idea why 
"test_3" don't pass?


require 'test/unit'
include System::IO
class Binstring < Test::Unit::TestCase
 def test_1
   fileName = 'flowers.jpg'

   #Ruby

   data1 = File.read(fileName)

   #.Net
   fs = FileStream.new(fileName, FileMode.Open, FileAccess.Read)
   br = BinaryReader.new(fs)
   numBytes = FileInfo.new(fileName).Length
   buff = br.ReadBytes(numBytes)
   data2 = String.CreateBinary(buff)

   assert data1 == data2
 end

 def test_2
   fileName = 'flowers.jpg'

   #Ruby

   data1 = File.read(fileName)

   #.Net
   fs = FileStream.new(fileName, FileMode.Open, FileAccess.Read)
   data2 = "".Append(fs, fs.length)

   assert data1 == data2
 end

 def test_3
   data0 = "This is a test\n"
   File.open('test.txt', 'w') {|f| f.write(data0) }
   data1 = File.read('test.txt')
   fs = FileStream.new('test.txt', FileMode.Open, FileAccess.Read)
   br = BinaryReader.new(fs)
   numBytes = FileInfo.new('test.txt').Length
   buff = br.ReadBytes(numBytes)
   data2 = String.CreateBinary(buff)

   assert_equal data0, data1
   assert_equal data0, data2
   assert_equal data1, data2
 end
end

-----------

Loaded suite binstring
Started
..F
Finished in 0.46875 seconds.

 1) Failure:
test_3(Binstring)
   [binstring.rb:45:in `test_3'
    c:/Ironruby/lib/IronRuby/test/unit/testcase.rb:79:in `run'
    :0:in `send'
    testrunner.rb:66:in `start_mediator'
    :0:in `each']:
<"This is a test\n"> expected but was
<"This is a test\r\n">.

3 tests, 4 assertions, 1 failures, 0 errors



On Fri, 23 Oct 2009 03:56:38 +0200, Tomas Matousek 
<tomas.matou...@microsoft.com<mailto:tomas.matou...@microsoft.com>> wrote:
In this particular case, I would recommend to use .NET API:
include System::IO
=> Object
stream = FileStream.new("c:\\temp\\a.txt", FileMode.Open, FileAccess.Read)
=> System.IO.FileStream
"".Append(stream, stream.Length)
=> "hello"

Tomas
_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org<mailto:Ironruby-core@rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core

_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to