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
value
doesn't have a trailing newline,value.chomp
won't change anything because the newline doesn't come fromvalue
but fromputs
:IO#write
is the method you're looking for.Finally,
f.truncate(0)
isn't needed becausew+
stands for :Example
returns :
With
puts
instead ofwrite
, it returns :Newlines inside the string ?
If for some reason, value is a String with newlines inside,
chomp
andwrite
won't help. Usegsub
to replace multiple newlines with just one :If you want spaces instead of newlines :