k52yha
Last Updated: February 25, 2016
·
2.832K
· excitedubai
Me

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."
end

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!" 
end

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

Say Thanks
Respond

2 Responses
Add your response

12244
Koala

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 ·
12251
Me

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 ·