Last Updated: February 25, 2016
· excitedubai

Copy to user's clipboard, in a webapp with Ruby

This is how you can copy anything, to a user's clipboard, even in a website or webapp.
This is all with the power of Ruby! :D

I'm going to be using Sinatra for this example, to make it as easy as possible!

This is uses an awesome gem called Clipboard!
You're going to have install it first.

Here's the app:

require 'clipboard'
require 'sinatra'

get '/' do 
"Hello there."

get '/copy' do 
Clipboard.copy("This is a sentence that has been copied to your clipboard")
"Copied some text to your clipboard! CTRL + P it!" 

And that's it! Atleast for a basic app.
Feel free to tweet at me, and let me know what you think there!

2 Responses
Add your response


I'm still a ruby noobie, but how does this work on a website? If Ruby is running on the server how can it access the clients clipboard while they are in the middle of viewing a web page?

over 1 year ago ·

It's on of Sinatra's specialities. It's much more complication in Rails to do the same thing. Sinatra incredibly gives you the ability for the server to interact with the client. They can this Middleware. Middleware is a feature of Rack, which is specific to Ruby.

over 1 year ago ·