Redirect puts to file?
-
Since the ruby console some times stops updating if it's an intensive command, I'd like to output puts statement to a file, instead of the Ruby console. Is that possible?
-
You can put this in your script, but don't leave it there!!
module Kernel alias_method ;real_puts, ;puts def puts(s) ; real_puts("real puts ; #{s}") ; end end puts "Hi there" ;
Inside the overridden method, you can do anything you want - write to a file, add a time stamp, create a webdialog and make your own ruby console, whatever you want. I show it to call the real puts method, but that doesn't have to happen.
-
Thanks Todd!
-
Kernel#puts is much more complex. it would be better using the splat operator:
def puts *arg #... end
You also could switch $stdout.
azuby
-
@azuby said:
end[/code]You also could switch $stdout.
-
$stdout points to an IO object. A File object is an IO object too:
File.ancestors #=> [File, IO, File::Constants, Enumerable, Object, Kernel]azuby
-
file = File.new(ErrorLogFile_Path_Name,"w") file.puts("MyErrorText...")
?
-
I just came across this new functionality of SU7 as posted on the API blog back in November:
Send ruby console output to the standard out
If you start up SketchUp from the command line, you can pipe to standard error and see ruby puts statements appear for you.
Sketchup.exe > myRubyLog.txtI have not tried it, but that seems like what you were looking for.
Chris
-
Interesting. It gives a bunch of error messages from scripts that initializes. But it didn't seem to pipe any errors when I typed in 5 / 0 in the console.
When I used one of my plugins that uses the puts method, it didn't update the file automatically, but only when I closed down SU. Not 100% sure how this works. But it sure is useful to catch startup errors. LibFredo got some for instance.
Advertisement