[Ironruby-core] byte[] to ruby string

Håkon Clausen hakonhc at hclausen.net
Fri Oct 23 08:31:34 EDT 2009


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.Matousek at 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


More information about the Ironruby-core mailing list