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

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.

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

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/

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

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.

Related

Issue with indentation in HAML-"rest of document was indented using 2 spaces"

I would have posted this in a Github Gist or using Tinkerbin, but the indentation appears WAY off when I copy and paste my HAML.
The error message is: Inconsistent indentation: "\t\t " was used for indentation, but the rest of the document was indented using 2 spaces.
I posted an imgur to show you where the error is happening, the browser is telling me line 8, as you can see in the photo.
Bear with me, this is probably an easy fix but I am very new to HAML.
If you're using SublimeText, add the following to Packages/User/Preferences.sublime-settings
"tab_size": 2,
"translate_tabs_to_spaces": true
... then re-paste the code and all tabs will be changed to spaces.
I think you should use either space or tab,
or you can select all and convert into spaces once finish your code, SUBLIME text editor is best for it
If you're using Ruby on Rails, make sure to make the indentation consistent with application.html.haml in views/layouts

Running a system command (pdflatex) within my Rails Controller

So I have a Rails application that upon user submission should generate some kind of a .tex file based on the user input, compile it into a pdf, and deliver the pdf. Through process of elimination, I am pretty positive that everything is working except for one line; the one where pdflatex is called.
Here's the vital snippet of code:
(If it matters, its located in the Questions controller under the generate action, which is called after the form sends the relevant information. Though this may not be the best way, I'm pretty certain its not the cause of the error)
texHeader = 'app\assets\tex\QuestionsFront.txt'
texOut = 'app\assets\tex\Questions.tex'
#copy latex header to new file
FileUtils.cp(texHeader, texOut)
File.open(texOut, 'a+') do |fout|
fout.write("\n")
# a loop writes some more code to fout (its quite lengthy)
fout.write("\\end{enumerate}\n")
fout.write("\\end{document}")
#The problem line:
puts `pdflatex app/assets/tex/Questions.tex --output-directory=app/assets/tex`
end
filename = 'Questions.pdf'
filelocation = "app\\assets\\tex\\" + filename
File.open(filelocation, 'r') do |file|
send_file file, :filename => filename, :type => "application/pdf", :disposition => "attachment"
end
end
Here's my reasoning: It generates the .tex file correctly, and given a pre-created Questions.pdf file it sends it just fine. When the command in the puts is copied to the terminal, it runs without a hitch (the file begins with \nonstopmode so no worries about small errors). But for some reason, when I run the above script, not even a log file is created with an error.
What am I overlooking? Any ideas? Any way of seeing what output the puts line is creating?
Thanks so much in advance!
Figured out my own problem. The error is pretty interesting. You'll see I'm calling
puts `pdflatex app/assets/tex/Questions.tex --output-directory=app/assets/tex`
within the block
File.open(texOut, 'a+') do |fout|
where just a few lines prior
texOut = 'app\assets\tex\Questions.tex'
Basically, I'm trying to get latex to compile a document while the file is still open. So long as I'm in the File.open block, the file is open, and its automatically closed upon the end of the block.
Cutting and pasting the line of code down below the end of the block made it work just like I wanted. However, for the sake of clarity and the rare case where someone else has this problem, its actually better to open a separate system shell, navigate to the directory where the latex document is and do the compiling there. So, my updated code looks like:
fout.write("\\end{document}")
end
system 'runlatex.bat'
where that batch file is as follows:
cd app/assets/tex
pdflatex Questions.tex
That way any additional files in the tex directory are found, the log file is created there, etc.
Reason why I never got a log file? The pdflatex never executed - the OS stopped it with a permissions error before it ever ran.
Hope this helps!
Backticks (and %x{}) provide the same parsing context as a double quoted string. That means that the usual backslashed escape sequences are interpreted inside backticks; in particular, \t is a tab so this:
puts `pdflatex app\assets\tex\Questions.tex --output-directory=app\assets\tex`
will end up with two tabs in and that breaks everything. You can start escaping your backslashes (I think you'll need two or three backslashes to get one down to the shell) or switch to normal slashes (which Windows usually accepts in paths):
puts `pdflatex app/assets/tex/Questions.tex --output-directory=app/assets/tex`
Alternatively, you could switch to open3 to avoid the escaping and quoting issues and also get better error handling capabilities.

Tokenize .htaccess files

Bet you didn't see this coming? ;)
So, a project of mine requires that I specifically read and make sense out of .htaccess files.
Sadly, searching on Google only yields the infinite woes of people trying to get their own .htaccess to work (sorry, couldn't resist the comment).
Anyway, I'm a bit scared of trying to get this thing out of open-source projects that use it. See, in the past few weeks, I ended up wasting a lot of time trying to fix my issues with this strategy, only to find out that I did better to read RFCs & specs and build the thing my way.
So, if you know about a library, or any (hopefully clean!) code that does this, please do share. In the mean time, if you know about any articles about .htaccess file format, I'm sure they'll be very handy. Thanks.
NB: I'm pretty much multilingual and could make use of any codebase, even though the end code will be Delphi. I know I'm asking too much, but I'd love to see less of C++. Just think of my mental health before sharing C++ code. :)
Edit: Well, I think I'm just going to do this manually myself. The file structure seems to be:
directive arg1 arg2 argN
<begin directive section>
</end directive section>
# single line comment
.htaccess grammar is actually the exact same as the Apache configuration itself, and example parsers do exist for it.
If you're looking to write your own, you are mostly correct on the format. Remember, section tags can be nested and can have parameters (like <Location />)
English method of parsing:
For each line in the file:
Strip whitespace from beginning and end of line.
If the line starts with a '#':
Parse it as a comment (or skip it)
Else, If the line starts with a '<':
If the next character is a '/', the line is a closing tag:
Seek to the next '>' to get the tag name, and pop it from the tag stack.
Else, the line is an opening tag:
Seek to the next '>' for the tag name.
If the tag, trimmed, contains whitespace:
Split on the first whitespace. The right side is params, left is the tag.
(IfModule, Location, etc use this)
Push the tag name to the tag stack.
Else, the line is a directive:
Split the line on whitespace. This is the directive and params.
Just add quote handling and you're set.

Notepad ++ highlights everything that comes after <%= in html.erb files

See the image above.
I'm working on notepad ++.
html.erb files are presented that way, and I don't know how to get rid of the sky blue highlighting that follows <%=.
zsalzbank, Ben and peterjwest are all correct. Here's some new information:
SciTE 2.29 has a version of SciLexer.dll that doesn't crash Notepad++ 5.9.3 and also interprets the single quote correctly.
I downloaded it from here: http://sourceforge.net/projects/scintilla/files/SciTE/2.29/ - the file you need is wscite229.zip
You can copy the new SciLexer.dll from the SciTE download into the Notepad++ folder.
UPDATE: Full instructions here: http://blog.dominicsayers.com/how-to-edit-erb-files-using-notepad/
I think your problem is the commented out end tag on that line. The %> is being commented out. Try using " instead of ' for your strings.
Here's a good article on it: http://therubyway.wordpress.com/2008/11/23/rails-on-notepad/
Essentially you just need to replace the scilexer.dll file (downloadable from the link) to fix the bugged <% %> syntax highlighting
Important Update:
Unfortunately this causes a crash on save (and will empty the file) on the new version of Notepad++, if you really want to use this it may work on an older version such as 5.8, you will have to test this.

Latex \tableofcontents command always shows blank Contents on first build

When I generate a .pdf file from a .tex file using pdflatex, only the "Contents" title is shown with no actual TOC. If I run pdflatex my.tex once more, it generates the TOC just fine. I can reproduce this simply by removing the .toc file. What I think is happening is that my .toc file is being generated too late -- so how can I make the TOC work first time? Should I be generating the TOC beforehand without using pdflatex?
This is normal. LaTeX document need several compilations to reach a stable state. Use rubber -d my to compile the right number of times (rubber comes as a package on many linux distros).
I might be mistaken, but I think, that this is the default behaviour. I assume, you also won't find correct cross references (footnotes, end notes, literature) after the first run of pdflatex.
The point is, that LaTeX needs the extra rounds to resolve the references pointing inside the document, to get numbering and page numbers right.
I experienced the same problem with the editor Latexian. What solved the problem was changing the preferences. I changed "Number of typesetter runs at end" to 3, instead of the default 1. Then I added the "Refresh" button to the toolbar and tried refreshing and it worked.

Resources