newb question?

D

Dirk Meijer

------=_Part_18603_26959768.1129912709560
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

hi everyone, i've got a noobish question..
is there a way to create a new file (html in this case) and write stuff int=
o
it?
there probably is..
i'm using Ruby 1.8.2 under windows xp
greetings, Dirk.

------=_Part_18603_26959768.1129912709560--
 
D

Dirk Meijer

------=_Part_18851_20231187.1129913799515
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

i read that, but don't really understand, how can i create a new file?
File.new tells me the file i try to create doesn't exist (well duh..)
and i also don't understand how to write into files.
maybe it's just me, but i often find the pickaxe hard to understand.
greetings, Dirk.

2005/10/21 said:
This is a bit out of date, as it's the first edition, but it's free, so
I try not to complain... the 2nd edition is well worth the money, I
recently purchased it from Amazon:

http://www.rubycentral.com/book/tut_io.html

This should get you started. Good luck!

-Will

------=_Part_18851_20231187.1129913799515--
 
D

Dirk Meijer

------=_Part_19999_17201527.1129918330789
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

wonderful how a little w can change my entire situation :)
greetings, Dirk.

2005/10/21 said:
Many ways - here's a simple one:


fname =3D 'C:\TEMP\dirk_2005_10_21.html'

File.open(fname, 'w') do |fo|
fo.puts('<HTML>')
fo.puts('</HTML>')
end

puts File.read(fname)

#-> <HTML>
#-> </HTML>


daz

------=_Part_19999_17201527.1129918330789--
 
B

Brian Schröder

Many ways - here's a simple one:


fname =3D 'C:\TEMP\dirk_2005_10_21.html'

File.open(fname, 'w') do |fo|
fo.puts('<HTML>')
fo.puts('</HTML>')
end

puts File.read(fname)

#-> <HTML>
#-> </HTML>


daz

I'm using debian, but as I understand it you should use forward
slashes under windows too. So

fname =3D 'c:/temp/dirk.html'

would be better. That also saves you from bugs like this one

fname =3D "c:\temp\dirk.html"

which are not so obvious to spot.

Regards,

Brian
 
D

Dirk Meijer

------=_Part_26585_12141239.1129982580752
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

now i have another question, is it possible to create a new directory,
because when i try that with File.open("./newdir/file.html", 'w') it gives
me an error.
so is it possible?
Dirk.

2005/10/22 said:
I'm using debian, but as I understand it you should use forward
slashes under windows too. So

fname =3D 'c:/temp/dirk.html'

would be better. That also saves you from bugs like this one

fname =3D "c:\temp\dirk.html"

which are not so obvious to spot.

Regards,

Brian

------=_Part_26585_12141239.1129982580752--
 
B

Brian Schröder

now i have another question, is it possible to create a new directory,
because when i try that with File.open("./newdir/file.html", 'w') it give= s
me an error.
so is it possible?
Dirk.
$ ri1.8 FileUtils
------------------------------------------------------- Class: FileUtils
(no description...)
------------------------------------------------------------------------

[snip]

Instance methods:
-----------------
cd, chdir, chmod, cmp, compare_file, compare_stream, copy,
copy_entry, copy_file, copy_stream, cp, cp_r, fu_blksize,
fu_check_options, fu_default_blksize, fu_each_src_dest,
fu_each_src_dest0, fu_list, fu_lstat, fu_mkdir, fu_output_message,
fu_same?, fu_stat, fu_stream_blksize, fu_update_option, getwd,
have_st_ino?, identical?, install, link, ln, ln_s, ln_sf, makedirs,
mkdir, mkdir_p, mkpath, move, mv, pwd, remove, remove_dir,
remove_file, rm, rm_f, rm_r, rm_rf, rmdir, rmtree, safe_unlink,
symlink, touch, uptodate?

$ ri1.8 FileUtils#mkdir
-------------------------------------------------------- FileUtils#mkdir
mkdir(list, options =3D {})
------------------------------------------------------------------------
Options: mode noop verbose

Creates one or more directories.

FileUtils.mkdir 'test'
FileUtils.mkdir %w( tmp data )
FileUtils.mkdir 'notexist', :noop =3D> true # Does not really creat=
e.
FileUtils.mkdir 'tmp', :mode =3D> 0700


$ irb
irb(main):001:0> require "fileutils"
=3D> true
irb(main):002:0> FileUtils.mkdir "test"
=3D> ["test"]
irb(main):003:0> File.open("test/t", "wb") do | f | f << "This is a test" e=
nd
=3D> #<File:test/t (closed)>
irb(main):004:0> File.read("test/t")
=3D> "This is a test"

$ rm -rf test

hope to help,

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

noob question 5
exerb and GUI's 4
Question about regular expression 12
RGSS 4
gui's for ruby? 5
method proposition 11
creating a <=> method 4
YARV 0.4.0 and ActiveRecord 1.13.2 rubygem 1

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top