On Windows, you have basically two choices for dealing with directory
names and paths.
You can use backslashes, like Windows expects. \ has special
meaning in Ruby strings, so you need to type it as \. e.g. “g:\Ruby”
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.
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/
also, is Ruby for Rails available to read online like the Pragmatic
Programmer’s Guide, or do I have to buy it?
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?
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
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.
This is the other book that I highly recommend:
…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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.