Any ideas why the following succeeds in creating a temp file but does not write to it?
def format_q_gen file = Tempfile.new([self.generator_file_name, ".rb"], "./lib/generators") file.write("Hello") end
Any ideas why the following succeeds in creating a temp file but does not write to it?
def format_q_gen file = Tempfile.new([self.generator_file_name, ".rb"], "./lib/generators") file.write("Hello") end
What makes you think it's not writing to it?
Hassan Schroeder wrote in post #1074521:
Interesting programming style
Personally, I'd close the file so it gets written to disk, and then look at it. FWIW.
Hassan Schroeder wrote in post #1074525:
I am placing an undefined method after file.write("Hello"). Crashes the program and leaves the file so I can open it - its empty...
Interesting programming style
Personally, I'd close the file so it gets written to disk, and then look at it. FWIW.
-- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com Hassan Schroeder | about.me twitter: @hassan
Yes, I'm learning to program on my own... I don't believe anyone would
pay for my work
Thanks, you were right - it is writing to the file and I can see it after closing. Can I follow with one last question...
I have written a method, saved it in a table, and this is what I am writing to the temp file. Can I require the file and then call the method (getting the returned arguments) from the temp file as it is saved as a .rb file? eg...
def format_q_gen file = Tempfile.new([generator_file_name, ".rb"], "./lib/generators") file.write(question) returned_question = generator_file_name # Calls the method in the tempfile formatted = { question: q["question_1"], correct_answer_letter: "a" } file.close file.unlink end
I would personally write that method to be a little safer IMO. Yeah it’s true that you can sometimes guarantee that problems won’t happen but relying on that slim chance is bad, it’s better to let file closing operations fall onto an ensure so that if something does go wrong then you can at least guarantee you aren’t sloppy about it.
def demo_tempfile
file = Tempfile.new([“hello_world”, “.rb”], “./lib/generators”)
file.write(“$stdout.puts ‘hello world’”)
file.rewind
$stdout.puts file.read # Will output the source without evaling.
ensure
file.close!
end
Or even
Tempfile.open([“hello_world”, “.rb”], “./lib/generators”) do |file|
#do stuff with file
end
which closes the file for you at the end of the block.
Fred
Frederick Cheung wrote in post #1074604:
file.rewind $stdout.puts file.read # Will output the source without evaling. # raise # Uncomment this line to see what I mean about ensure. ensure file.close! end
Or even
Tempfile.open(["hello_world", ".rb"], "./lib/generators") do |file| #do stuff with file end
which closes the file for you at the end of the block.
Fred
Thanks, trying to find out how to call a method contained in the file (method has same name as file)
Tempfile.open(["hello_world", ".rb"], "./lib/generators") do |file|
# If the file is a method, how can I call the method here and get the returned
arguments?
The file can't "be" a method, but it may contain a string describing a method. You can read that string and invoke it using `eval`.
See: Module: Kernel (Ruby 1.9.3)
HTH,
Hassan Schroeder wrote in post #1074693: