unable to find ::write for file class

Hello community! Today I am writing here because I am getting an error while trying to write in a file. Basically it says that write is not a valid method for file class.


Isn't File supposed to be a child of IO? if so, why doesn't it work?


Here is the code I used:


File.new(@f_name, 'r+') { |file| file.write("#{line}\n", i*(line.size)) }

#I also tried with this one

File.write(@f_name, "#{line}\n", i*(line.size))


they should do almost the same thing, but the problem is what I explained above. Could someone explain me why I get this error?


Forgive me if this is a dumb question but I am still learning ruby =)
Does 'r+' open the file for writing, or for appending?  Do you need to open it with 'w' in order to use write?


I normally use file.puts and it works okay.
'r+' is supposed to be read-write and it starts at the beginning of the file. Since I want to modify an existing file without having to reset it I cannot use 'w'. Appending should be 'a' or 'a+'.


That said File.write(filepath, string, number) is supposed  to write the given string at the number index without deleting what's after that string. You can find that method here.


@Shaz when using file.puts does it delete what's after the string you write or does it leave it as it is?
There is no argument for number of characters, so my guess is that it would replace it.
@Shaz Can I use it in a similar way of IO::write without slurping the entire file into memory?


I mean, if I read the whole file I can just do something like this:


fout = []
File.foreach(@f_name).with_index do |line, line_num|
if (line_num == what_i_need)
#here goes the code to change that line
end
fout << line
end
f = file.open(@f_name, 'w')
f.puts fout


But that is a problem for huge files...is there a better way which is not using another file either?
RGSS3 (VX Ace uses that) uses Ruby 1.9.2 version. The method you linked is from 2.0.0 version.


You can load the file, and operate it as a string afterwards, using the string.insert(nth, other) method on it. When you are done, you can save the modified file.


I don't think that there is any built-in way for this in this version of Ruby, at least I couldn't find any yet.


But I haven't read all of this yet: https://ruby-doc.org/core-1.9.2/IO.html


You might find something usable there.
tbh I don't know.  You said in your first post that File.write wasn't working for you, and I suggested trying File.puts instead because that's what I use and it works for me.  You didn't say anything about needing to replace just a part of the file while leaving the rest intact.  I don't know anything about that, so I can't really offer any other suggestions.
@Sixth thank you, that explains why it doesn't work in rpg maker.


So since there isn't something like that in ruby 1.9.2 I started experimenting and:

There is no argument for number of characters, so my guess is that it would replace it.



You guessed right, reading a file with 'r+' and using File.puts or file.print will replace the text. The only problem is that if you replace a line with two lines the "\n" (and as well the one included in puts) takes 2 characters. Of course if you use it to replace another "\n" it doesn't replace the next character and only replaces the "\n".


That's why it is possible in Ruby 1.9.2 to do something like that. Just in case someone is interested here is the equivalent code for that method:


f = File.open(@file_name, 'r+')
f.pos = character_number #this is the starting character
f.print(your_string)
f.close

#This will start writing your string replacing from the character_number character (included)


And thanks to both of you I find a solution and learned something new about Ruby. Thanks a lot guys!
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/unable-to-find-write-for-file-class.69980/