3

I'm trying to create and write to a new file using

@logFile = File.open("C:\Users\---\Desktop\mylog.log", "w+")

And nothing happens. My program uses

  @logFile.write ("Hello")
  @logFile.flush

And this line seems to be running ok (no crashes or anything) But i can't see any newly created file.

What am i missing out here?

1

2 Answers 2

2
  1. Your backslashes are escaped, in a string enclosed with double quotes you need to double them, or just use the Unix notation. So "C:\\Users\\---\\Desktop\\mylog.log"

or "C:/Users/---/Desktop/mylog.log"

or 'C:\Users\---\Desktop\mylog.log'

  1. Paths in Ruby are safest in Unix notation, so even when you use backslashes for ease of copying you are better to convert them to Unix formatting.

like this 'C:\Users\---\Desktop\mylog.log'.gsub('\\','/')

The double backslash is also needed here, the ' and \ need to be escaped using single quotes.

Another tip not relevant tot the question but very handy: use the block method to open a file so that it is clear when the file is closed, see this example

File.open(path, 'w') do |file|
  file.puts "Hello"
end

The file is closed after the end.

For logging though, take a look at logger, once you used it you won't stop using it.

Sign up to request clarification or add additional context in comments.

5 Comments

By the way: I believe, there should actually now be a file named Users---Desktopmylog.log in whatever the working directory of the C: drive was, when you ran your script, created by the original buggy version of your code. It's been a long time since I have done any Windows programming, though, so I could be wrong.
yes, hence no error, in such case a search with everything.exe would reveal where mylog.log is residing and had make the mistake obvious
"All paths in Ruby need to be in Unix notation". No, they don't. On Windows a Windows-style path is fine. Depending on how the string is defined the delimiters might need to be escaped. This is covered in the opening to the IO documentation. *nix-style delimiters are more convenient, and allow Ruby to translate between Windows and *nix systems transparently, but they're not required.
true, but as a windows ruby dev I encountered lots of problems in the past using windows style paths and so took on the habbit of using unix style paths, support for windows is defentitly rising the last years, I'll adapt my answer
You should use path = File.join("C:","Program Files","Blah") to create paths
-1

You should always use path = File.join("C:","Program Files","Blah") To ensure it works on any architecture.

2 Comments

This will fail on *nix, which has no idea of "C:".
I guess the point of my post was that you should always use File.join() to generate nested paths rather than strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.