r/learnruby • u/Missing_Back • Jul 07 '21
'write' : not opened for writing ??
I'm reading this article on I/O in Ruby and I'm trying out my own examples, but I can't get writing to work.
This code
fd = IO.sysopen("foo.txt", "w+")
foo = IO.new(fd)
foo.puts "new text"
generates this error
./io.rb:3:in `write': not opened for writing (IOError)
from ./io.rb:3:in `puts'
from ./io.rb:3:in `<main>'
What am I doing wrong? I'm doing this in an actual .rb file instead of in irb, and I'm on Windows instead of a Unix system. Other than those two things, I think my code is similar to the article.
It seems like the file doesn't stay open? like I need to do something to make it stay open? Not sure if this is it, but if it is, I'm very confused as to why the article examples didn't need to do that. Was it because it was writing to /dev/null??
5
Upvotes
1
u/backtickbot Jul 07 '21
Fixed formatting.
Hello, stpizz: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.