Reputation: 1467
I am reading an empty HTML-File like this:
file = File.read("file_path/file.html", "wb")
Why does that throw this TypeError?
no implicit conversion of String into Integer
Log:
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.0ms)
TypeError (no implicit conversion of String into Integer):
app/controllers/abc_controller.rb:49:in `read'
app/controllers/abc_controller.rb:49:in `build'
Upvotes: 2
Views: 1590
Reputation: 54223
If the file is empty, what do you want to read exactly?
The second parameter for File#read is optional, and should be the length of the String you want to read out of the file. "wb"
isn't an Integer, hence the error messsage.
The parameters you used look more like open
.
If you want to read the file, just use
content = File.read(filename)
If you want to write it, you can use
File.open(filename,'w+') do |file|
file.puts "content"
end
'w+'
is a file mode which :
Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
If you want to check that a file exists :
File.exists?(filename)
If you want to check that an existing file is empty :
File.size(filename)==0
The file could be full of whitespaces (size > 0, but still "empty"). With Rails :
File.read(filename).blank?
Upvotes: 5
Reputation: 416
For reading a file you can use rb instead of wb.
data = File.open("ur path","rb")
Upvotes: 0