# Files?

**URL:** <https://www.ruby-forum.com/t/files/70352>\
**Category:** Ruby\
**Created:** [September 28, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/files/70352 "2006-09-28T16:56:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 28, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/files/70352/1 "2006-09-28T16:56:34Z")

</div>

I’m getting quite lost and discouraged trying to learn Ruby. [I have no programming experiance, just html and css]

for something to start with, I would like to use ruby to create a file  
in directory G:\Ruby named “myFile.html”

that seems simple enough, but I can’t figure out how to do it [and  
[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_io.html) isn’t  
helping]

thanks 🙂

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 28, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/files/70352/2 "2006-09-28T17:38:30Z")

</div>

On 9/28/06, Jonathan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m getting quite lost and discouraged trying to learn Ruby. [I have no programming experiance, just html and css]
> 
> for something to start with, I would like to use ruby to create a file  
> in directory G:\Ruby named “myFile.html”
> 
> that seems simple enough, but I can’t figure out how to do it [and  
> [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_io.html) isn’t  
> helping]

On Windows, you have basically two choices for dealing with directory  
names and paths.

1. You can use backslashes, like Windows expects. \ has special  
meaning in Ruby strings, so you need to type it as \. e.g. “g:\Ruby”
2. You can use forward-slashes, which Ruby will convert to the  
appropriate thing on your platform.

#2 is a better choice, because #1 causes your code to be basically  
Windows-only.

Example code: (There are many, many ways to do this. This is just one  
of them.) You can either type this in after running the ‘irb’ command,  
or put it in a file called ‘example.rb’, and run it with: ruby  
example.rb

Dir.chdir “g:/Ruby”  
puts “Now we are in: #{Dir.pwd}”  
File.open(‘my\_file.html’, ‘w’) do |file|  
file.puts “”  
file.puts “other stuff”

# etc, etc, etc.

end

The section betewen the ‘do’ and the ‘end’ is called a block. In this  
case, Ruby will automatically make sure the file is closed for you  
when leaving the block. Convenient, because it saves you a whole bunch  
of error checking.

I recommend that you pick up ‘Ruby for Rails’, and ‘Programming Ruby,  
2nd Edition’ (a.k.a. The Pickaxe) to help get you started.  
Even if you aren’t interested in Rails, don’t be put off by the title  
of ‘Ruby for Rails’. It is an excellent book that just happens to use  
Rails as its example code.

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 28, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/files/70352/3 "2006-09-28T17:42:31Z")

</div>

Jonathan D. wrote:

> thanks 🙂

File.open(“g:/Ruby/myFile.html”,File::WRONLY|File::CREAT) do |f|  
f \<\< “”  
f \<\< "  
f \<\< “Hello World”  
f \<\< “”  
f \<\< “”  
end

?

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 28, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/files/70352/4 "2006-09-28T22:35:52Z")

</div>

#opening a file for writing  
my\_file = File.new(‘g:/ruby/myFile.txt’, ‘w’)

# if you tried to open the file like this and the file didn’t exist

already this would fail  
my\_file = File.new(‘g:/ruby/myFile.txt’, ‘r’)

The modes are platform dependant - I’m assuming Windows.

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 29, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/files/70352/5 "2006-09-29T19:09:47Z")

</div>

Wilson B. wrote:

> The section betewen the ‘do’ and the ‘end’ is called a block. In this  
> case, Ruby will automatically make sure the file is closed for you  
> when leaving the block. Convenient, because it saves you a whole bunch  
> of error checking.
> 
> I recommend that you pick up ‘Ruby for Rails’, and ‘Programming Ruby,  
> 2nd Edition’ (a.k.a. The Pickaxe) to help get you started.  
> Even if you aren’t interested in Rails, don’t be put off by the title  
> of ‘Ruby for Rails’. It is an excellent book that just happens to use  
> Rails as its example code.

It worked! Thank you very much. is this one of the books you are talking  
about?  
[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)  
also, is Ruby for Rails available to read online like the Pragmatic  
Programmer’s Guide, or do I have to buy it?

thanks again

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 28, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/files/70352/6 "2006-09-28T22:31:52Z")

</div>

Well, there are a couple of ways to do that depending on what you’re  
planning on doing with the file. Can you give some of the code you’re  
trying? Or errors?

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 29, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/files/70352/7 "2006-09-29T20:49:31Z")

</div>

On 9/29/06, Jonathan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > of ‘Ruby for Rails’. It is an excellent book that just happens to use  
> > Rails as its example code.
> 
> It worked! Thank you very much. is this one of the books you are talking  
> about?  
> [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)  
> also, is Ruby for Rails available to read online like the Pragmatic  
> Programmer’s Guide, or do I have to buy it?
> 
> thanks again

That’s the book, but it’s the 1st edition, and doesn’t cover Ruby 1.8.  
I highly recommend purchasing the the 2nd edition. You can buy it as  
hardcopy or PDF.

> **[Programming Ruby (2nd edition)](https://pragprog.com/titles/ruby/index.html)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

This is the other book that I highly recommend:

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails?new=true&experiment=A)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

…also available as hardcopy or PDF.

If I had to pick one, I would choose ‘Ruby for Rails’. Luckily, they  
are both pretty affordable as programming books go.
