Created
April 30, 2011 14:30
-
-
Save ahoward/949714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put this in your .irbrc and then you can do | |
# | |
# irb-prompt > data = api.call | |
# irb-prompt > c data.to_json | |
# | |
# which will copy the data to your clipboard. general usage is | |
# | |
# c(stuff, stuff, stuff) | |
# | |
# | |
module Kernel | |
private | |
def c(*args) | |
buf = args.join | |
fork{ fork{ IO.popen('pbcopy', 'w'){|fd| fd.write(buf)} }; exit!(0) } | |
end | |
end |
- fork a child
- fork a grandchild
- the child exits without calling the normal exit handles, which would otherwise cause the child to be zombied waiting on the execution of it's grandchild - aka the child completely abandons the grandchild
- grandchild completes the work
this is some of the 'standard' practice of creating a daemon:
ref:
Advanced Programming in the Unix Environment
W. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7.
Nice for OSX.. for linux just use
xself --clipboard --input insteast of pbcopy :)
Oh and nice to add also
def paste()
fork{ fork{ IO.popen('pbpaste', 'w'){|fd| fd.write(buf)} }; exit!(0) }
end
or xsel --clipboard --output
moving all of this into a gem -> https://github.com/ahoward/irbcp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd love to know what the nested
#fork
calls get you and why you're explicitly callingexit!(0)
. I understand at a high level what this code does, but the nuance is lost on me.