Force newline only on Windows

S

Steve V

How can I force \n in my strings to be *only* a newline character and not a
carriage return/newline combination on Windows?

Thanks,
Steve
 
S

Shajith

Hi 'talk!

Is there anything in Rubyland like SciPy?

URL: http://www.scipy.org/About/

In short, it is a collection of scientific tools, combining a lot of
numeric functions, data visualization/plotting routines etc into a
single package. (The part I liked was about how it can transform the
Python command line interpreter to a very capable Math/Analysis tool)

Does any combination of Ruby libraries offer the same sort of functionality?

Thanks in advance!
Shajith

PS: Apologies if this sounds like another of those "Ruby vs Python" things.

PS1: Right now, I am aware of Narray. I'm also looking for some more
functionality(optimization?, plotting, etc)
 
R

Robert Klemme

Steve V said:
How can I force \n in my strings to be *only* a newline character and not a
carriage return/newline combination on Windows?

It's always just a newline in strings. I guess you mean you want the \n
in a file written:

09:14:10 [temp]: irb
irb(main):001:0> File.open("x","wb") {|io| io.puts "foo\nbar"}
=> nil
irb(main):002:0> exit
09:15:51 [temp]: od -t c x
0000000 f o o \n b a r \n
0000010
09:15:56 [temp]:

Kind regards

robert
 
G

Glenn Parker

Steve said:
How can I force \n in my strings to be *only* a newline character and not a
carriage return/newline combination on Windows?

Use "binary" mode when opening your file, or invoke the "binmode" method
on your IO object.

File.open("x.txt", "wb") do |file|
file.print "\n"
end

On Windows, x.txt will contain only "\n". If you leave out the "b", you
will get "\r\n" instead.

STDOUT.binmode
print "\n"

This will make the same change to stdout.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top