Skip to content

Instantly share code, notes, and snippets.

@elgalu
Last active January 29, 2018 16:33
Show Gist options
  • Save elgalu/5940941 to your computer and use it in GitHub Desktop.
Save elgalu/5940941 to your computer and use it in GitHub Desktop.
IRB / Pry copy last result, a.k.a `_`, to your OSX / Linux / Windows clipboard using the 'clipboard' gem
require 'clipboard' #gem install clipboard
def cbcopy(value)
Clipboard.copy value unless value.empty?
flash_message = value.empty? ? "Sorry, nothing to copy" : "Copied to clipboard"
print "# #{flash_message}: "
puts %Q("#{value}")
end
# Copy last result to the Clipboard
def cp
cbcopy(IRB.CurrentContext.last_value.to_s)
end
# Copy last exception details to the Clipboard
def cpe
value = idk #TODO please help me fix this, $! doesn't work
cbcopy(value.nil? ? "" : value.inspect)
end
require 'clipboard' #gem install clipboard
def cbcopy(value)
Clipboard.copy value unless value.empty?
flash_message = value.empty? ? "Sorry, nothing to copy" : "Copied to clipboard"
_pry_.output.print text.green("# #{flash_message}: ")
_pry_.output.puts %Q("#{value}")
end
Pry.commands.block_command("cp", "Copy last result to the Clipboard", :listing => "cp") do |line|
cbcopy(_pry_.last_result.to_s)
end
Pry.commands.block_command("cpe", "Copy last exception details to the Clipboard", :listing => "cpe") do |line|
value = _pry_.last_exception
cbcopy(value.nil? ? "" : value.inspect)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment