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