Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Created May 17, 2011 12:56
Show Gist options
  • Save bil-bas/976418 to your computer and use it in GitHub Desktop.
Save bil-bas/976418 to your computer and use it in GitHub Desktop.
logging to file and output
workmad3: Spooner: tbh, if you did pretty much exactly the same thing as I just did, but grabbed the stdout reference, and opened the file into the two ivars, it would work
[12:46] banisterfiend: sweet_horse = SweetHorse.new
[12:46] workmad3: banisterfiend: woo! infinite recursion!
[12:46] Spooner: Yeah workmad3's answer seems more sensible :)
[12:46] banisterfiend: workmad3: STDOUT.puts blah
[12:46] banisterfiend: ;)
[12:46] workmad3: ;)
[12:47] Spooner: Thanks. I'll probably do that then :)
[12:47] injekt: pretty sure stdout depends on a write method, banisterfiend
[12:47] workmad3: Spooner: banister's method is exactly the same as mine, but creates the puts method explicitly rather than hijacking method_missing
[12:47] Spooner: Yeah, yours seemed a bit more useful :) banisterfiend is such a noob!
[12:48] workmad3 hides from banisterfiend's wrath
[12:48] banisterfiend goes to watch asian porn instead
[12:48] workmad3: Spooner: what would be more useful would be if the method_missing defined the methods on the instance the first time they were called...
[12:48] workmad3: Spooner: but that's left as an exercise for the reader ;)
[12:49] Spooner: I'm not too bothered about performance on that level.
[12:49] Spooner: But yeah, that would make sense.
[12:49] workmad3: Spooner: fair enough... depends on how much you're logging out tbh
[12:50] injekt: Spooner: https://gist.github.com/dae8468ea815bff23686
[12:50] injekt: and you dont only want a puts method, that wont work, there must be a write method
[12:50] workmad3: Spooner: of course, the other option would be to log out to a file, and then use 'tail -f' in a separate window to look at it ;)
[12:50] Spooner: The time to look up the methods is minute compared to the time to actually write to tdisk though.
[12:50] injekt: (puts does a write under the hood)
[12:51] Spooner: workmad3: Yeah, users are more picky than I am. Not my idea :)
[12:51] workmad3: injekt: remember, the puts method on the class is forwarding to the puts method on the stdout object
[12:51] Spooner: injekt: Thanks, but workmad3s example manages all methods fine, I think (method_missing).
[12:52] injekt: workmad3: Not sure I follow, this wont work: https://gist.github.com/dae8468ea815bff23686
[12:52] workmad3: Spooner: hmm... injekt's solution has one advantage... it doesn't hold an open file handle between writes...
[12:52] Spooner: That too :)
[12:52] workmad3: injekt: err... yes it will
[12:52] injekt: Spooner: also, 'all methods' meaning what? All methods rely on write, so you only need that/
[12:52] injekt: workmad3: it doesn't work here
[12:53] workmad3: injekt: STDOUT.puts "Hi" just spat out "Hi" when I did it in IRB
[12:53] injekt: $stdout calls write explicitly, its a typeerror
[12:53] injekt: well yeah
[12:53] injekt: that works
[12:53] injekt: try my example.. you cant assign a class which doesn't respond to write, yours works ofc because of method missing
[12:54] injekt: you only need to overwrite write though, because the IO methods all use that under the hood
[12:54] injekt: so I see no point in method_missing myself
[12:54] injekt: unless I'm missing something
[12:54] injekt: (pun intended)
[12:54] workmad3: ah, it's stuff happening under the hood in Kernel#puts
[12:54] injekt: IO.puts
[12:54] injekt: calls io write
[12:54] workmad3: whichever :)
[12:55] workmad3: so yeah... if it only needs write, then that would make the most sense... just provide a single write method
[12:55] injekt: all IO methods rely on read/write (print, puts, etc)
[12:55] injekt: oops lag
[12:56] workmad3: I did my original example for Logger, rather than just replacing stdout, and I couldn't be bothered to write out all the explicit forwarding :)
[12:56] injekt: :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment