copy string to pasteboard

231 views
Skip to first unread message

Daniel Higginbotham

unread,
May 10, 2010, 6:16:15 PM5/10/10
to boston-r...@googlegroups.com
This code will copy a string to your pasteboard (for mac users):

def pbcopy(string)
IO.popen('pbcopy', 'w'){|p| p << string; p.close_write}
end

I put it in my .irbrc file and use it when I'm trying to debug some code and want to take a closer look at a string outside of terminal

--
You received this message because you are subscribed to the Boston Ruby Group mailing list
To post to this group, send email to boston-r...@googlegroups.com
To unsubscribe from this group, send email to boston-rubygro...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/boston-rubygroup

Brian Cardarella

unread,
May 10, 2010, 6:25:38 PM5/10/10
to boston-r...@googlegroups.com
Daniel,

That's a cool trick. I tend to use the backticks when running
system commands in Ruby. Anything in backticks can accept
interpolation. You could rewrite like this:

def pbcopy(string)
`pbcopy #{string}`
end


- Brian

Daniel Higginbotham

unread,
May 10, 2010, 6:37:36 PM5/10/10
to boston-r...@googlegroups.com
In this case you need to send text to pbcopy via standard input, and if you did something like `#{string} | pbcopy` you'd likely get an error like "sh: foo: command not found" . The code below will just hang because pbcopy is waiting for input :D

Michael Breen

unread,
May 10, 2010, 7:01:38 PM5/10/10
to boston-r...@googlegroups.com
You can also use %x{} for system commands which I like to use cause I think it stands out just a little more than backticks do.

Best,
Mike


On May 10, 2010, at 6:25 PM, Brian Cardarella wrote:

Wyatt Greene

unread,
May 10, 2010, 11:27:34 PM5/10/10
to boston-r...@googlegroups.com
I like %x{} as well.  An obscure fact about Ruby is that you can use other characters instead of {}, so that these are equivalent:

puts `ls`
puts %x{ls}
puts %x|ls|
puts %x-ls-
puts %x&ls&

Now that you know that, forget you ever learned it and never use it in your code. :)

Brian Cardarella

unread,
May 11, 2010, 12:33:06 AM5/11/10
to boston-r...@googlegroups.com
Daniel,

My bad, it should be:

def pbcopy(string)
`echo '#{string}' | pbcopy`
end

%x{} is definitely cleaner too.

- Brian

Steve Harris

unread,
May 11, 2010, 9:47:59 AM5/11/10
to boston-rubygroup
When using popen() or %s{}, remember that (a) stderr is not
redirected, and (b) the child command is launched via the shell; I've
run into subtle bugs which traced back to these factors.

- Steve

Keenan Brock

unread,
May 11, 2010, 2:58:32 PM5/11/10
to boston-r...@googlegroups.com
Daniel,

I found that I don't always have access to a string (a). sometimes I want to take standard out (b).

#a
copy('string to clipboard')

#b
copy do
  puts "list of words:"
  %w{strings for the clibpoard}.each { |s| puts s }
end


So here is the version I use

    # thanks:
    # stick in .irbrc
    def copy(str=nil)
       if block_given?
         old_stdout = $stdout
         output_str = StringIO.new
         $stdout = output_str
         begin
            yield
         ensure
            $stdout = old_stdout
         end
         str=output_str.string
       end
       IO.popen('pbcopy', 'w') { |f| f << str.to_s } unless str.nil?
    end

FWIW/
Keenan

Daniel Higginbotham

unread,
May 11, 2010, 3:03:08 PM5/11/10
to boston-r...@googlegroups.com
very cool, thanks!
Reply all
Reply to author
Forward
0 new messages