Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save string to file

I've got a iteration (in my view):

([email protected]_i).each do |i|
   ...doing things...
  @bigtable << @result[0..result.length-2]
end

Every @result is a string. @bigtable has every @result from iterations. Now what I want: I want to save content from the @bigtable (after clickin a button) to .csv file (and choose where to save it on my hdd). And I want do it like every @result from @bigtable is in its own single line, like this (pseudo code):

@result string from @bigtable[0]
@result string from @bigtable[1]
etc.

Please, help

like image 432
lukaszkups Avatar asked Jan 05 '12 13:01

lukaszkups


People also ask

How do I save a text file in Python?

First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.

How do you write a string of text into a file in C?

You can use int fprintf(FILE *fp,const char *format, ...) function as well to write a string into a file.

Which is used to write a string to a file?

fputs() is used to write a string to a file.


1 Answers

To transform your array into a string you can do :

@bigtable.join("\n")

To write this string into a file :

File.open("path/to/file", "w") { |file| file.write @bigtable.join("\n") }

And that's it!

BTW:

@result[0..result.length-2] == @result[0..-2]
like image 147
Mathieu Mahé Avatar answered Oct 17 '22 04:10

Mathieu Mahé