PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Line ending problem.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Line ending problem.

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 00h42   #1
sea__chan@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Line ending problem.

Hi

I am writing a ruby script to insert some content into the beginning of
some files.

The files would be used in Windows or Mac, when I use File.puts method
to write the file, it changes the line ending of the file.

Any way to let ruby keep the previous line-ending of the file?

Thanks
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 01h03   #2
jeremywoertink@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Line ending problem.

Cong Chan wrote:
> Hi
>
> I am writing a ruby script to insert some content into the beginning of
> some files.


>
> Thanks


So you want to add stuff to the beginning of a file? Well, as far as I
know, you can't just add stuff to the beginning, you have to create a
new file, add your stuff, then append the old stuff at the end of the
new file.

new_file = File.new("your_new_file.txt", "w+")

new_file.write("I'm at the beginning\n")

IO.readlines("your_old_file.txt").each do |line|
new_file.write(line)
end

new_file.close


~Jeremy
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 01h08   #3
Xavier Noria
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Line ending problem.

On Nov 6, 2007, at 1:42 AM, Cong Chan wrote:

> I am writing a ruby script to insert some content into the beginning
> of
> some files.
>
> The files would be used in Windows or Mac, when I use File.puts method
> to write the file, it changes the line ending of the file.
>
> Any way to let ruby keep the previous line-ending of the file?


Not automatically.

If you know before hand that the line-ending convention is uniform you
can open the file in binary mode, pick a few bytes, and look for the
first line separator (=~ /\015?\012/).

Then write in binary mode and set $\ to that capture, aka the ouput
record separator. Use print instead of puts, since puts does not check
$\ but prints "\n" unconditionally.

-- fxn


  Réponse avec citation
Vieux 06/11/2007, 15h16   #4
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Line ending problem.

On 11/5/07, Xavier Noria <fxn@hashref.com> wrote:

> Then write in binary mode and set $\ to that capture, aka the ouput
> record separator. Use print instead of puts, since puts does not check
> $\ but prints "\n" unconditionally.


I'd suggest that rather than this you use write instead of print or
puts and put the line breaks in explicitly.

My concern over using $\ is that it's global so it affects any other
Io objects, and I'm not sure it's a thread-safe global.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h22.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08286 seconds with 12 queries