The problem I have is that whenever I save some values to a File, Rails magically adds trailing whitespace to each line.
For example when I read the file the initaial content got manipulated:
"cv" => "cv\r\n"
"cv\r\nvd" => "cv\r\n\r\nvd\r\n"
Here is how I create my File:
File.open(path, "w+") do |f|
f.truncate(0)
f.puts value
end
I appreciate each help!
Theory
If
valuedoesn't have a trailing newline,value.chompwon't change anything because the newline doesn't come fromvaluebut fromputs:IO#writeis the method you're looking for.Finally,
f.truncate(0)isn't needed becausew+stands for :Example
returns :
With
putsinstead ofwrite, it returns :Newlines inside the string ?
If for some reason, value is a String with newlines inside,
chompandwritewon't help. Usegsubto replace multiple newlines with just one :If you want spaces instead of newlines :