Read the contents of a local file in a variable in Rails

All I want to do is get all the content from the local file and save it in a variable. How?

File.read(@icon.full_filename).each {|l| r += l}

only gives me some of this. In PHP, I just used file_get_contents .

+58
ruby ruby-on-rails
Jun 16 '10 at 16:35
source share
3 answers

Answering my own question here ... it turns out that this is just a Windows ghost that occurs when reading binary files (in my case JPEG), which requires an additional flag in the call to the open or File.open function. I revised it to open("/path/to/file", 'rb') {|io| a = a + io.read} open("/path/to/file", 'rb') {|io| a = a + io.read} , and everything was in order.

+13
Jun 16 '10 at 17:29
source share
 data = File.read("/path/to/file") 
+110
Jun 16 '10 at 16:37
source share

I think you should use IO.binread("/path/to/file") if you have the latest ruby ​​interpreter (ie> = 1.9.2)

You can find the IO class documentation here http://www.ruby-doc.org/core-2.1.2/IO.html

+12
Aug 08
source share



All Articles