22

I discovered from here that if you have a script you want to run in the rails console, you sometimes have to copy paste it line by line (copy pasting it all at once doesn't always work)

This is very tedious for lengthy scripts

Is there a work around or faster way?

Example - this will not copy paste from text editor to console:

class Article
    def initialize(title, link, paragraphs)
        @title = title
        @link = link
        @paragraphs = paragraphs 
    end
    attr_reader :title
    attr_reader :link
    attr_reader :paragraphs
end

Edit The above snipped does copy paste right into the rails console. But when I grab the same text from sublime text 3, it errors after the second line, with: Display all 522 possibilities? (y or n)..

The Answer I worked out why. My script (in sublime text) used tabs as indents. The rails console only accepts spaces as indents. That's an hour of my life I won't get back. I hope this saves someone else some time.

3
  • 1
    Copy/paste of multiple lines works just fine in my console.
    – jvillian
    Commented Apr 17, 2018 at 0:22
  • @jvillian thanks for the help it turned out I had tab indents and rails console only likes spaces
    – stevec
    Commented Apr 17, 2018 at 0:37
  • 2
    Glad you saw the issue was tabs (always use spaces with Ruby, and indent by two spaces). You may also run into a limit on how many characters can be pasted at once, due to limitations of the tty. (although that's not what happened here)
    – anothermh
    Commented Apr 17, 2018 at 3:05

4 Answers 4

38

This issue (pasting multi-line code into irb on the console, on a Mac, using iTerm) bugged me for a long time and finally found the solution.

In my case the issue was with iTerm. It turns out iTerm by default pastes the content at a speed that is too fast for readline, the library that irb uses to read input.

The solution was to do Edit > Paste Special > Paste Slower, twice.

See here for a similar case: https://gitlab.com/gnachman/iterm2/issues/3607

15

SOLUTION

Open the rails console with this option:

rails console -- --nomultiline

You can paste many lines of code without problem then.

EXPLANATION

The problem here is that IRB wants to write a letter at a time. You can disable this behaviour by giving up the ability to move the cursor up and down when you write and edit a block of code before closing it.

Source: https://tosbourn.com/speed-up-pasting-text-into-rails-console/

2
  • Yes. That resolved a slowness (and even an out-of-memory) issue I was having with pasting into rails console. I'm on Ruby 2.7.2. Looks like there's more discussion of this issue here: github.com/ruby/irb/issues/43 and maybe it's fixed in the later ruby versions?
    – Harry Wood
    Commented Jul 26, 2023 at 23:31
  • What a legend! Thanks Commented Sep 6 at 14:25
1

I can't comment because of reputation, so i add an answer about a 'tips' that can save some of your time.

In most of the text editor / IDE used to write code you can choose to replace the tabulation by an amount of space. It's a good thing to do so to avoid the tabulation characters in files raising some errors like yours ;)

https://www.sublimetext.com/docs/3/indentation.html

0

For me replacing tabs to spaces wasn't enough, maybe because the content i was trying to paste was so large. What worked for me was removing all unneeded spaces, replacing newlines with semicolons, and replacing do...end blocks with one line curly bracket blocks. Basically getting everything into as few lines as possible.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.