How might I pass text data from the ruby console into my clipboard without saving to a file?
You can just echo it instead if there are no newline characters in the string; otherwise, use the IO
class.
Using echo
:
system "echo #{stringdata} | pbcopy"
OR
`echo #{stringdata} | pbcopy`
Ruby will then just rip the text from memory, inject it into the shell command which opens a pipe between the echo
and pbcopy
processes.
Using the IO
class:
If you want to do it the Ruby way, we simply create a pipe with pbcopy
using the IO class. This creates a shared files between the processes which we write to, and pbcopy
will read from.
IO.popen("pbcopy", "w") { |pipe| pipe.puts "Hello world!" }
use ruby in WSL to copy text to clipboard
Thanks to @max for the insight. I was able to come up with a solution that does what I need it to, here's the code:
exec( "echo '#{data2}' | clip.exe" )
this code snippet takes the value stored inside data2
and copys it to the clipboard. When the script has finished running in my Ubuntu WSL, I can paste and the value on my clipboard is the value that was contained in data2
when the script was running.
Hope this helps anyone else with this question.
Unable to save pbcopy to a new file without opening a text-editor
Try: pbpaste >newFile
Running keystrokes from program
If sending arbitrary keystrokes to other applications is what you're after you can use the gem https://github.com/erinata/auto_click for it. However, if you can't use gems, what you can do instead is run NirCmd (or one of its alternatives) with the appropriate command line arguments to achieve the same result.
For example:
# Tell the OS to bring up the Notepad window and give it the time
# to do so.
`nircmd win activate ititle notepad`
sleep 0.5
# Select all text in the Notepad window and copy it to the
# clipboard.
`nircmd sendkeypress ctrl+a`
`nircmd sendkeypress ctrl+c`
In-place progress output in the terminal or console
Use carriage return. '\r' should usually work.
How to write to file in Ruby?
The Ruby File class will give you the ins and outs of ::new
and ::open
but its parent, the IO class, gets into the depth of #read
and #write
.
Pipe to/from the clipboard in a Bash script
2018 answer
Use clipboard-cli. It works with macOS, Windows, Linux, OpenBSD, FreeBSD, and Android without any real issues.
Install it with:
npm install -g clipboard-cli
Then you can do:
echo foo | clipboard
If you want, you can alias to cb
by putting the following in your .bashrc
, .bash_profile
, or .zshrc
:
alias cb=clipboard
Related Topics
Why Is This String Key in a Hash Converted to a Symbol
Mapping Values from Two Array in Ruby
Gem Dependencies Versions Meaning
Using the Postgresql Gem Async
How to Use an Overridden Constant in an Inheritanced Class
How to Use an Actionview::Helper in a Ruby Script, Outside of Rails
Is Autoload Thread-Safe in Ruby 1.9
Why Doesn't Ruby Have a Threadpool Built-In
Ruby: How to Chain Multiple Method Calls Together with "Send"
$ Bundle Exec Rake Db:Reset Command Raising Couldn't Drop Db/Development.Sqlite3
How to Call Rake Tasks That Are Defined in the Standard Rakefile from an Other Ruby Script
Generating Unique Token on the Fly with Rails
How to Replace Multiple Newlines in a Row with One Newline Using Ruby
Getting a Dns Txt Record in Ruby
Does Scala Scale Better Than Other Jvm Languages
Ruby/Rails Using || to Determine Value, Using an Empty String Instead of a Nil Value