Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy paste multiple lines of code into rails console (e.g. copy paste from a script)

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.

like image 208
stevec Avatar asked Apr 17 '18 00:04

stevec


People also ask

How do you copy multiple lines?

Press Ctrl + C twice in a row while Word, Excel, PowerPoint, or another Office app is open, and the Office Clipboard will keep the most recent 24 items you've copied.

How do I run a Ruby script in rails console?

Run source code from the editor in a console Open the required Ruby file in the editor (if necessary, select a fragment of code to be executed). From the main menu, choose Tools | Load file/selection into IRB/Rails console.


1 Answers

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

like image 138
Nico Brenner Avatar answered Oct 24 '22 17:10

Nico Brenner