Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/irb/command/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ def execute(arg)
private

def copy_to_clipboard(text)
IO.popen(clipboard_program, 'w') do |io|
io.write(text)
if Gem.win_platform?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a flag --disable-gems

$ ruby --disable-gems -e binding.irb      
irb(main):001> Gem
uninitialized constant Gem (NameError)

I think it's better to add a guard like defined?(Gem) &&

Kernel.system("powershell.exe", "-NoProfile", "-Command", "Set-Clipboard", "-Value", text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, passing text via stdin does not work reliably in IRB

This might be fixed in ruby/reline#875
Although I also think adding this workaround for a while is worth. What do you think?

else
IO.popen(clipboard_program, 'w') do |io|
io.write(text)
end
end

raise IOError.new("Copying to clipboard failed") unless $? == 0
Expand All @@ -54,6 +58,8 @@ def copy_to_clipboard(text)
def clipboard_program
@clipboard_program ||= if IRB.conf[:COPY_COMMAND]
IRB.conf[:COPY_COMMAND]
elsif executable?("clip.exe")
"clip.exe"
elsif executable?("pbcopy")
"pbcopy"
elsif executable?("xclip")
Expand All @@ -66,7 +72,7 @@ def executable?(command)
end

def clipboard_available?
!!clipboard_program
Gem.win_platform? || !!clipboard_program
end
end
end
Expand Down