-1

I'm pretty new to Ruby. I want to create a file by writing:

file.new "test.txt", "w"

And de result is:

NameError: undefined local variable or method 'file' for main:Object
from (irb):4
from c:/Ruby21/bin/irb:11:in '<main>'

I don't know what it means. And another question if I may: How do I say in what folder I want to save the file?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

3 Answers3

1

You are writing file with a lower case f, which means you are accessing a local variable or a method. In order to access the File class, you must write it with a upper case F:

File.new 'test.txt', 'w' do |file|
  file.write 'Some text.'
end
Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
0

It should be File, not file.

File.new "test.txt", "w"
falsetru
  • 357,413
  • 63
  • 732
  • 636
0

file isn't defined, you have to use File:

File.new "test.txt", "w"
Undo
  • 25,519
  • 37
  • 106
  • 129