views:

73

answers:

2

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

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

only gives me a part of it. In PHP, I just used file_get_contents.

Thanks!

+2  A: 
data = File.read("/path/to/file")
zed_0xff
I thought that, but that gives me a string of length 52. The actual file size when I go File.size("/path/to/file") is 1676.
Steven Xu
+1  A: 

Answering my own question here... turns out it's a Windows only quirk that happens when reading binary files (in my case a JPEG) that requires an additional flag in the open or File.open function call. I revised it to open("/path/to/file", 'rb') {|io| a = a + io.read} and all was fine.

Steven Xu
Unless you're actually concatenating a bunch of files together, I'd just write that as: `data = File.open("file", "rb") {|io| io.read}`
glenn jackman