unable to find ::write for file class

Status
Not open for further replies.

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
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 =)
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
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.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
'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?
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
There is no argument for number of characters, so my guess is that it would replace it.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
@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?
 
Last edited by a moderator:

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
823
First Language
Hungarian
Primarily Uses
RMVXA
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
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.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
@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!
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,381
Reaction score
8,537
First Language
English
Primarily Uses
RMMV
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.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,048
Messages
1,018,545
Members
137,834
Latest member
EverNoir
Top