[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

writing to a file converting it needs a thread ?

pere.noel

12/27/2005 12:12:00 AM

i have a small script building an xml string and, afterwards, i need to
convert the string.

basically i do :
xml=""
album_list.each do |k,l|
xml=xml+"<name first=\"#{l[0]}\" last=\"#{l[1]}\" company=\"#{l[2]}\"
organization=\"#{l[3]}\"/>\n"
end
tmpFile.puts xml

system("iconv -c -s -f MACROMAN -t UTF-8 #{tmpName} >
#{xmlDir}/#{xmlName}")


and the resulting conversion lacks of one half a line to 26 lines,
depending the number of lines (some how randomly))


if after having done that, i launch iconv from command line by :
iconv -c -s -f MACROMAN -t UTF-8 /tmp/AddressBook-MACROMAN.xml >
AddressBook.xml

("same" command as system("..."))

i get all the 156 lines ???

does than means system("iconv -c ...") is started within the ruby script
before the end of "tmpFile.puts xml" in which case i'd have to sync
writing by puts and reading by iconv ?

if this is true, how to do that ?
--
une bévue
2 Answers

Patrick Fernie

12/27/2005 12:30:00 AM

0

From your snippet it looks like you need to be calling .close (if
you're done working with the file) or .flush (if not) on tmpFile
before you call an external command on it; otherwise, the File is left
open and the contents aren't necessarily in sync with what you've
puts'd. When you exit the ruby process, however, the File is
automatically flush'd, so it is then in sync, so when you call iconv
from your prompt, it works as expected.
-P

On 12/26/05, Une bévue <pere.noel@laponie.com> wrote:
> i have a small script building an xml string and, afterwards, i need to
> convert the string.
>
> basically i do :
> xml=""
> album_list.each do |k,l|
> xml=xml+"<name first=\"#{l[0]}\" last=\"#{l[1]}\" company=\"#{l[2]}\"
> organization=\"#{l[3]}\"/>\n"
> end
> tmpFile.puts xml
>
> system("iconv -c -s -f MACROMAN -t UTF-8 #{tmpName} >
> #{xmlDir}/#{xmlName}")
>
>
> and the resulting conversion lacks of one half a line to 26 lines,
> depending the number of lines (some how randomly))
>
>
> if after having done that, i launch iconv from command line by :
> iconv -c -s -f MACROMAN -t UTF-8 /tmp/AddressBook-MACROMAN.xml >
> AddressBook.xml
>
> ("same" command as system("..."))
>
> i get all the 156 lines ???
>
> does than means system("iconv -c ...") is started within the ruby script
> before the end of "tmpFile.puts xml" in which case i'd have to sync
> writing by puts and reading by iconv ?
>
> if this is true, how to do that ?
> --
> une bévue
>
>


pere.noel

12/27/2005 8:18:00 AM

0

Patrick Fernie <patrick.fernie@gmail.com> wrote:

> From your snippet it looks like you need to be calling .close (if
> you're done working with the file) or .flush (if not) on tmpFile
> before you call an external command on it; otherwise, the File is left
> open and the contents aren't necessarily in sync with what you've
> puts'd. When you exit the ruby process, however, the File is
> automatically flush'd, so it is then in sync, so when you call iconv
> from your prompt, it works as expected.

ok, it's flush doing the job in my case, many thanks ;-)
<code>
also what is the best practice, writing :
xml=""
album_list.each do |k,l|
xml=xml+"<name first=\"#{l[0]}\" last=\"#{l[1]}\" company=\"#{l[2]}\"
organization=\"#{l[3]}\"/>\n"
end
tmpFile.puts xml
tmpFile.flush
<code>

or that :
<code>
album_list.each do |k,l|
tmpFile.puts "<name first=\"#{l[0]}\" last=\"#{l[1]}\"
company=\"#{l[2]}\" organization=\"#{l[3]}\"/>\n"
end
tmpFile.flush
<code>
that's to say writing to the file the whole result at once or each line
after each line ?

and also, if, after :
<code>
system("iconv -c -s -f MACROMAN -t UTF-8 #{tmpName} >
#{xmlDir}/#{xmlName}")
<code>

i suppress the temporary file like that :
<code>
system("rm -f #{tmpName}")
<code>

i get a warning :
-----------------
../AddressBook2vCardXml.rb:56: warning: Insecure world writable dir
/Users/yvon/work/Ruby/rubyaeosa-0.2.3/sample/, mode 040777
-----------------
for this latest line
the perms in the working folder being :
-rw-rw-rw- 1 yvon yvon 12387 Dec 27 08:56 AddressBook.xml
for files
and :
-rwxrwxrwx 1 yvon yvon 1687 Dec 27 08:56 AddressBook2vCardXml.rb
for ruby scripts.

if i change the perms to :
-----------------
-rwxr--r-- 1 yvon yvon 12387 Dec 27 08:56 AddressBook.xml
-rwxr--r-- 1 yvon yvon 1687 Dec 27 08:56 AddressBook2vCardXml.rb
-----------------
that's to say only user:user as right to write|exec a file in that
folder, i get the same warning with the same "mode 040777" ???


also in the /tmp directory the file perms is only :
-----------------
-rw-r--r-- 1 yvon wheel 12340 Dec 27 09:07
AddressBook-MACROMAN.xml
-----------------

seems to be correct for me ))
i thought it was special methods in ruby, in order to get tmp file name
???
--
une bévue