Working with Ruby
Occasional blog posts about Ruby updates, tools, editor tweaks, and random snippets. You might also be interested in my newer project that docuements lesser-known features in Ruby: Idiosyncratic Ruby.

Access the system clipboard with JRuby

This is my first jruby code (written for the clipboard gem) and I was quite surprised that you only need one-liners ;)

 1
2
3
4
5
6
7
def copy(text)
  Java::JavaAwt::Toolkit.default_toolkit.system_clipboard.set_contents(Java::JavaAwtDatatransfer::StringSelection.new(text), nil)
end

def paste
  Java::JavaAwt::Toolkit.default_toolkit.system_clipboard.get_data(Java::JavaAwtDatatransfer::DataFlavor.stringFlavor)
end
Creative Commons License

juwi | April 04, 2012

Looks good. Been toying with it, too a little. JRuby is Java done right ;-)