Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created April 30, 2011 14:30
Show Gist options
  • Save ahoward/949714 to your computer and use it in GitHub Desktop.
Save ahoward/949714 to your computer and use it in GitHub Desktop.
# 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
@lmarburger
Copy link

I'd love to know what the nested #fork calls get you and why you're explicitly calling exit!(0). I understand at a high level what this code does, but the nuance is lost on me.

@ahoward
Copy link
Author

ahoward commented Apr 30, 2011

http://stackoverflow.com/questions/881388/what-is-the-reason-for-performing-a-double-fork-when-creating-a-daemon

  1. fork a child
  2. fork a grandchild
  3. 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
  4. 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.

@epinault
Copy link

epinault commented May 2, 2011

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

@ahoward
Copy link
Author

ahoward commented May 3, 2011

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