2

Problem: I cannot paste multi-line code as pry tries to execute every line.

Expected behavior:

[1] pry(main)> %w[a b c]
.map(&:to_sym)
=> [:a, :b, :c]
[2] pry(main)>

Actual behavior:

[1] pry(main)> %w[a b c]
=> ["a", "b", "c"]
[2] pry(main)> .map(&:to_sym)
sh: -c: line 0: syntax error near unexpected token `&'
sh: -c: line 0: `map(&:to_sym)'
Error: there was a problem executing system command: map(&:to_sym)
[3] pry(main)>

Code is just a short example to explain the problem. This problem occurs when I copy some other multiline code snippet from a code editor to a console.

Please, do not suggest using EnforcedStyle: trailing rubocop rule and rewrite the project. I prefer to use EnforcedStyle: leading (default)

Environment:

  1. iTerm2 Build 3.5.2 - I'm not sure if it's connected with iTerm2, as in the standard Terminal app for a macbook it behaves the same.
  2. asdf version manager.
  3. rails 7.0.2.3 - I'm not sure if it's connected with Rails. I tried just globally installed pry gem and it behaves the same.
  4. pry 0.14.1 - I don't use any file with specific configuration.
  5. pry-rails 0.3.9 - I'm not sure if it's connected with pry-rails. I tried just globally installed pry gem and it behaves the same.

I asked ChatGPT and googled about the problem and tested all I found. Nothing helped.

The most interesting part is that I'm in progress of migration from my previous macbook air m1 to my new macbook pro m2. I didn't use Migration Assistant app for that. The Air was my first apple computer and I was studying using their OS. I wanted to configure the new computer and setup the project there manually from the ground up. So the Expected behavior works on Air, and doesn't work on Pro. I compared ~/.zshrc and the only one difference there related to the way I installed asdf. "What could be easier when you have a properly configured machine to just copy the settings you need?" thought I. Okay, not so easy...

4
  • 2
    Strange, I can paste multi-line code into irb / pry (or any other repl) in Terminal and iTerm perfectly fine. And I'm pretty sure it worked out of the box. Maybe it's your readline version / configuration? How did you install Ruby? Commented Jun 24, 2024 at 15:47
  • 2
    It appears this is a known issue regarding the replacement of Readline with Reline (ruby 3.3): github.com/pry/pry/issues/2063, github.com/pry/pry/pull/2298 Commented Jun 24, 2024 at 15:53
  • @Stefan I used the asdf version manager on both computers: 1. installed the plugin, installed ruby. There are several versions installed on my old machine, but the same version with the new machine is set as global (3.1.1). I didn't know anything about readline/reline. Thank you and engineersmnky for the clue - I'll investigate that Commented Jun 24, 2024 at 17:07
  • @Stefan seems like you are right: I installed the latest ruby with the latest version of Pry on Macbook Pro and multiline paste works there like a charm. I don't know how to fix the issue without upgrading ruby/rails for the project and I still don't understand why does it work on another computer with the same ruby/rails/pry versions Commented Jun 24, 2024 at 17:49

2 Answers 2

3

From the hints provided by Stefan and engineersmnky in the comments, the main culprit seems to be the Reline gem, not directly pry per se.

Therefore to fix multiline pasting, if your pry or irb is using Reline, upgrading to the latest Reline version should fix the issue:

> pry
[1] pry(main)> Reline::VERSION
=> "0.4.1"
[2] pry(main)> %w[a b c]
=> ["a", "b", "c"]
[3] pry(main)> .map(&:to_sym)
sh: 1: Syntax error: "&" ....

> gem install reline
> pry
[1] pry(main)> Reline::VERSION
=> "0.5.9"
[2] pry(main)> %w[a b c]
.map(&:to_sym)
=> [:a, :b, :c]
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for the answer! But it doesn't help to fix the issue in the rails console of my project without updating all related dependencies (I have no separate line for the reline gem in my gemfile, it's just a dependency). I also checked in the Rails console reline gem version on both computers and it is the same. Again, I have no idea why it works differently on two machines and how to fix that without refactoring the whole project or without spending time on updating dependencies. I know - it's important to keep versions fresh, but have different priorities at the moment.
@AlexeyZalyotov. What is the Reline version you are seeing in the Rails console? How about you just try to add it manually to your Gemfile, and put 0.5.9 there as a requirement. See if Bundler can sort out the dependency?
@AlexeyZalyotov Also, are you at least able to create a blank Rails project with a working console on the troubled machine? If you try to get something simple to work first, perhaps we can get some clues what the culprit is.
This worked for me. Annoying that it doesn't work properly in the first place. For me, I ran bundle update reline and took a quick look at Gemfile.lock. Restarted pry and everything worked how it should. Thanks for the research and great answer, and thanks @AlexeyZalyotov for asking (questions like this are hard to ask because they're so nebulous and could be caused by so many different things).
This was giving me grief again. pry would be fine when it received >1 line of code via copy/paste, but would error when receiving >1 line of code via vim-slime. I updated reline but no different. I added this to .vimrc and it works now let g:slime_bracketed_paste = 1
0

I don't know the cause of the issue - I'm also seeing the paste work fine on a Mac but not in Linux.

Here's a workaround that shouldn't be too annoying:

Add this to the bottom of your .pryrc file (reference). This tells pry to disable dot commands.

Pry.commands.delete /\.(.*)/

Then whenever you want to paste multi-line code, type an open parenthesis before pasting, and close it when done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.