Newb question?

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

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

Dirk Meijer wrote:

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

Many ways - here's a simple one:

fname = '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 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, Will <wrbriggs@gmail.com>:

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

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

···

2005/10/21, daz <dooby@d10.karoo.co.uk>:

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

Many ways - here's a simple one:

fname = '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

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

Many ways - here's a simple one:

fname = '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 = 'c:/temp/dirk.html'

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

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

which are not so obvious to spot.

Regards,

Brian

···

On 21/10/05, daz <dooby@d10.karoo.co.uk> wrote:

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

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, Brian Schröder <ruby.brian@gmail.com>:

On 21/10/05, daz <dooby@d10.karoo.co.uk> wrote:
>
> Dirk Meijer wrote:
> > hi everyone, i've got a noobish question..
> > is there a way to create a new file (html in this case)
> > and write stuff into it?
> > there probably is..
> > i'm using Ruby 1.8.2 under windows xp
> > greetings, Dirk.
>
>
> Many ways - here's a simple one:
>
>
> fname = '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 = 'c:/temp/dirk.html'

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

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

which are not so obvious to spot.

Regards,

Brian

>
>
>
>

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

$ ri1.8 FileUtils
------------------------------------------------------- Class: FileUtils
     (no description...)

···

On 22/10/05, Dirk Meijer <hawkman.gelooft@gmail.com> wrote:

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.

------------------------------------------------------------------------

[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 = {})
------------------------------------------------------------------------
     Options: mode noop verbose

     Creates one or more directories.

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

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

$ rm -rf test

hope to help,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/