Write to tempfile

76 views
Skip to first unread message

Dave Castellano

unread,
Sep 3, 2012, 10:08:27 PM9/3/12
to rubyonra...@googlegroups.com
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

--
Posted via http://www.ruby-forum.com/.

Hassan Schroeder

unread,
Sep 3, 2012, 10:42:23 PM9/3/12
to rubyonra...@googlegroups.com
On Mon, Sep 3, 2012 at 7:08 PM, Dave Castellano <li...@ruby-forum.com> 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 Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Dave Castellano

unread,
Sep 3, 2012, 10:57:28 PM9/3/12
to rubyonra...@googlegroups.com
Hassan Schroeder wrote in post #1074521:
I am placing an undefined method after file.write("Hello"). Crashes the
program and leaves the file so I can open it - its empty...

Hassan Schroeder

unread,
Sep 3, 2012, 11:07:43 PM9/3/12
to rubyonra...@googlegroups.com
On Mon, Sep 3, 2012 at 7:57 PM, Dave Castellano <li...@ruby-forum.com> 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 :-)

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

Dave Castellano

unread,
Sep 4, 2012, 7:27:15 AM9/4/12
to rubyonra...@googlegroups.com
Hassan Schroeder wrote in post #1074525:
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

Jordon Bedwell

unread,
Sep 4, 2012, 7:49:29 AM9/4/12
to rubyonra...@googlegroups.com
On Tuesday, 4 September 2012 06:27:52 UTC-5, Ruby-Forum.com User wrote:
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.
      # raise # Uncomment this line to see what I mean about ensure.
    ensure
      file.close!
    end
 

Frederick Cheung

unread,
Sep 4, 2012, 8:05:46 AM9/4/12
to rubyonra...@googlegroups.com
On Tuesday, September 4, 2012 12:49:29 PM UTC+1, Jordon Bedwell wrote:
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

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       

Dave Castellano

unread,
Sep 4, 2012, 12:37:09 PM9/4/12
to rubyonra...@googlegroups.com
Frederick Cheung wrote in post #1074604:
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?

Hassan Schroeder

unread,
Sep 4, 2012, 3:28:11 PM9/4/12
to rubyonra...@googlegroups.com
On Tue, Sep 4, 2012 at 9:37 AM, Dave Castellano <li...@ruby-forum.com> 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: http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-eval

HTH,

Dave Castellano

unread,
Sep 4, 2012, 7:17:27 PM9/4/12
to rubyonra...@googlegroups.com
Hassan Schroeder wrote in post #1074693:
Sorry, meant the file contains a method...

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

DC
Reply all
Reply to author
Forward
0 new messages