Write to tempfile

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

On Mon, Sep 3, 2012 at 7:08 PM, Dave C. [email protected]
wrote:

Any ideas why the following succeeds in creating a temp file but does
not write to it?

What makes you think it’s not writing to it?


Hassan S. ------------------------ [email protected]

twitter: @hassan

Hassan S. wrote in post #1074521:

On Mon, Sep 3, 2012 at 7:08 PM, Dave C. [email protected]
wrote:

Any ideas why the following succeeds in creating a temp file but does
not write to it?

What makes you think it’s not writing to it?


Hassan S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan
I am placing an undefined method after file.write(“Hello”). Crashes the
program and leaves the file so I can open it - its empty…

I am actually trying to write a string attribute from a database table
to the tempfile but can’t even write the “Hello” string…

Hassan S. wrote in post #1074525:

On Mon, Sep 3, 2012 at 7:57 PM, Dave C. [email protected]
wrote:

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 :slight_smile:

Personally, I’d close the file so it gets written to disk, and then look
at it. FWIW.


Hassan S. ------------------------ [email protected]
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 :slight_smile:

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([gen_file_name, “.rb”], “./lib/gens”)
file.write(question)
require file
returned_question = gen_file_name # Calls method in tempfile
formatted = {
question: q[“question_1”],
correct_answer_letter: “a”
}
file.close
file.unlink
end

On Mon, Sep 3, 2012 at 7:57 PM, Dave C. [email protected]
wrote:

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 :slight_smile:

Personally, I’d close the file so it gets written to disk, and then look
at it. FWIW.


Hassan S. ------------------------ [email protected]

twitter: @hassan

On Tuesday, 4 September 2012 06:27:52 UTC-5, Ruby-Forum.com User wrote:

method (getting the returned arguments) from the temp file as it is
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.
  # raise # Uncomment this line to see what I mean about ensure.
ensure
  file.close!
end

On Tuesday, September 4, 2012 12:49:29 PM UTC+1, Jordon B. wrote:

  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

On Tue, Sep 4, 2012 at 9:37 AM, Dave C. [email protected]
wrote:

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 S. ------------------------ [email protected]

twitter: @hassan

Frederick C. wrote in post #1074604:

On Tuesday, September 4, 2012 12:49:29 PM UTC+1, Jordon B. wrote:

  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?

end

Hassan S. wrote in post #1074693:

On Tue, Sep 4, 2012 at 9:37 AM, Dave C. [email protected]
wrote:

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 S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

Sorry, meant the file contains a method…

Thanks for the help!! I think that is what I was looking for.

DC