[Ironruby-core] byte[] to ruby string

Tomas Matousek Tomas.Matousek at microsoft.com
Fri Oct 23 11:22:36 EDT 2009


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

Tomas

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Ivan Porto Carrero
Sent: Friday, October 23, 2009 5:47 AM
To: hakonhc at hclausen.net; ironruby-core at 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 <hakonhc at hclausen.net<mailto:hakonhc at 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.Matousek at microsoft.com<mailto: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
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091023/daf54883/attachment-0001.html>


More information about the Ironruby-core mailing list