How to check entire rails project for compilation

2020-08-25 00:30发布

问题:

I am new to ruby and rails. I am used to working in IDEs (Xcode, Visual Studio, etc.) where I can perform project-wide/workspace-wide "build/compile" operations.

Let's say that I modify a number of ruby files in my rails project. I haven't yet written tests that will exercise all of my changes.

Is there a way to ensure that all of my *.rb files compile without directly exercising them at runtime? I'd really just like to perform a "compile all of my ruby/erb files" operation so that I know that I don't currently have any syntax errors.

UPDATE

I probably should have mentioned that I've been writing code professionally for 20 years. I realize that Ruby isn't compiled like, say, C++, but that doesn't mean that its syntax can't be checked. In my case, I've decided to use ruby-lint to catch basic syntax errors without having to exercise the code at runtime.

回答1:

Ruby is an interpreted language. There is no compilation step. Please follow TDD and write your tests before you write your code.

UPDATE: Since this answer was accepted, I can't delete it unless the OP accepts another answer but people landing here through Google search, please see the other answers. This was more of a tongue in cheek remark than an answer. Ruby community has wonderful tools for doing all kinds of static code analysis like Rubocop and friends. At the bare minimum, ruby -c will accomplish what the OP wanted.



回答2:

If you're on Linux or Mac or some funny windows box with the gnu find command, you can do something like:

find /your/application_directory -name=*.rb -exec ruby -c {} \;

This will find all ruby scripts in the app directory and run ruby -c on them, which will run a syntax check and respond with Syntax OK or an error message if there is an error in the script.

You can also create a macro of this to your editor, so that when you save or press a key combination it will run ruby -c on the file and give you the results. In vim you can use this in your .vimrc :

map <Leader>c :w !ruby -c<cr>

Which maps Leader-c to write the file and check it's syntax using ruby -c.

Verifying your rake-tasks and views for syntax errors might be a little trickier, but many of the templating engines like haml have similiar -c syntax check parameter.



回答3:

This a one-line answer from @Zac comment, I have included it as an answer as not all the time we do read the comments:

find . | grep ".*\.rb$" | xargs -L 1 ruby -c


回答4:

Why can't you continue to use an IDE like RubyMine (or its father, IntelliJ)?

There are also plugins for text editors that will highlight syntax issues, but who knows what you're using. I use Sublime Text 2 for non-IDE development and it supports several varieties of automatic linting. As does Emacs, but I'm guessing you won't go there.

Whether you use "real" TDD or not, you need to tighten your code-writing cycles and work more incrementally so you don't have a chance to accumulate such high-level (low-level?) errors in the first place, though. That's true regardless of the language or environment.



回答5:

You can even do

find . -type f -name '*.rb' -or -name '*.ru' -or -name '*.rake' > /tmp/list.txt while read file ; do ruby -c $file done < /tmp/list.txt

to check your entire project