Could someone please tell me how to create a new file?
Thank you.
Could someone please tell me how to create a new file?
Thank you.
Andy Ferra wrote:
Could someone please tell me how to create a new file?
You could use FileUtils.touch or just File.open a filename
in write-mode. You can take a look at the documentation for
those classes by typing
ri FileUtils
ri File
On your command-line.
Thank you.
E
Hi,
For an empty file, I guess the easiest way would be:
begin
f=File::open(“test.txt”,“w”)
rescue SystemCallError
puts “Problem with File::open #1”
#… something went wrong…
else
f.close
end
begin
File::open("/etc/test.txt",“w”) do | f |
# Do somethinf with “f” …
end
rescue SystemCallError
# The file can’t be created
puts “Problem with File::open #2”
end
begin
File::open(“test.txt”,“w”) {}
rescue SystemCallError
# The file can’t be created
puts “Problem with File::open #3”
end
read “ri IO::open” for more info!
Merc.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs