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/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Sep 3, 2012 at 7:08 PM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder wrote in post #1074521:> On Mon, Sep 3, 2012 at 7:08 PM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > 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.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassanI am placing an undefined method after file.write("Hello"). Crashes the program and leaves the file so I can open it - its empty... -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Sep 3, 2012 at 7:57 PM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder wrote in post #1074525:> On Mon, Sep 3, 2012 at 7:57 PM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > 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. > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassanYes, 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 -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/sXQZ1lknkrYJ. For more options, visit https://groups.google.com/groups/opt_out.
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Frederick Cheung wrote in post #1074604:> On Tuesday, September 4, 2012 12:49:29 PM UTC+1, Jordon Bedwell 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. > > FredThanks, 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 returnedarguments?> end-- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Sep 4, 2012 at 9:37 AM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder wrote in post #1074693:> On Tue, Sep 4, 2012 at 9:37 AM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > 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, > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassanSorry, meant the file contains a method... Thanks for the help!! I think that is what I was looking for. DC -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.