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       

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/MvGE_ooVHUMJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to