How to check entire rails project for compilation errors - ruby-on-rails

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.

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.

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

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.

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

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.

Related

Find all uses of a method in vim

Is there a way to find all uses of a method in vim? I'm using vim as an IDE for Rails with rails.vim. ctags helps to jump to a method definition from usage but not the other way round AFAIK. I'd like to be able to find everywhere (controllers, views etc) that a method has been used.
There aren't any perfect solutions for this in Vim, but you can get close with cscope and grep or ack.
cscope will help you find all references to a symbol. It's made for C and C-like languages, but it does a decent job of matching symbols in Ruby code as well. It's not going to get the context right all the time.
Here's a vim cscope tutorial and a blog post about Ruby/Vim/cscope, and another blog post, both of which include additional tips about navigating Ruby/Rails code in Vim.
Using grep or ack from Vim with quickfix integration is another great way to find symbols. They've got no notion of scope/context, but often a simple search is enough. Using just the built-in :grep command, you can do:
:grep some_method app/controllers
:cwindow
And get the results of the search in the quickfix window, which will allow you to quickly navigate to the matching files and line numbers.
A much better option is the ack.vim plugin, which integrates ack with Vim and makes use of the quickfix window.
If you're not already using a plugin for navigating CTAGS, I recommend Tagbar.
I wrote a gem to do just that: https://rubygems.org/gems/starscope
It parses ruby code properly and exports to ctags and cscope file formats.

Ruby on Rails command line looks weird

I just began learning RoR using the materials from railstutorial.org. When the author uses the command line, the resulting text are always nicely organized and colored. When I do the same thing, I often get "weird" characters and unorganized text as result.
http://i.imgur.com/2Q0kzwf.jpg
Example
http://i.imgur.com/mZP4SI9.jpg
My attempt to do the same
I'm not quite sure what to do to make my command line more organized like the one shown in the tutorial. Any help would be appreciated. In case you need to know, I'm using Windows 7.
It is because by default Windows command prompt doesn't know about ANSI color Sequences. You can try https://github.com/adoxa/ansicon which is supposed to make it aware. The reason the tutorial authors look like that is they are using a *nix shell that understands the color codes (probably bash or zsh on either linux or osx if I had to guess)
As Doon pointed out, the Windows console doesn't recognize ANSI sequences. As suggested your best bet right now is to use external tools.
However, since there are some pain points using most of the external tools, Ruby 2.0 will provide support for ANSI escape codes in Ruby out-of-the-box without having to depend on external gems or tools.

How do I incorporate an independently maintained ruby script into a rails application

I have a ruby script (.rb) that opens a file and makes a csv file after some parsing.
I maintain the script independently and may use in other applications.
Right now I just copied and pasted the code into my controller... I know that isn't right!
How am I supposed to incorporate this ruby script to my application?
Do I make it a gem?
Thanks.
Making it a gem and installing it is one option. Otherwise, register the directory of the file as load path. That part may depend on the operating system. For example, with Ubuntu Linux, I do in the terminal:
export RUBYLIB=path_to_the_directory_where_the_file_is
Then, require that file and use it. When you want your library to behave differently depending on if it was called directly from the command or from another ruby script, the common way is to write in your library:
if __FILE__ == $0
commands_to_execute_when_called_directly_from_command
end
We write little Ruby-based command-line tools all the time, and treat them as regular Linux apps. It's trivial to call them using back ticks or %x, or chain them using regular pipes (|) as we would a regular app.
If we're going to be throwing a lot of data around, often we'll add a --json flag using OptionParser, which lets us emit JSON to the calling program, making it easier to reuse the data instead of having to parse CSV or columnar data.
You can install those sort of apps in /usr/local/bin on a *nix system, make sure the path is set to search there, and then share the code among shell, Ruby or any other language capable of using a sub-shell.
Just because they're written in Ruby doesn't mean they have to be a gem or module. Ruby is capable of much more than that and fits into the usual host ecology well.

What components make VIM a good (great) ruby editor?

I'm learning ruby on rails on a linux box and dusting off my VIM skills (skillz?).
When I got started on VIM way back in my c++ days, I had a friend with a great vimfiles folder that had tons of stuff to get started. Starting from scratch, vim is great, but it feels like it could be a lot better.
I currently have:
vim-ruby
buffer explorer
xml-edit (though I don't have it currently working with erb files)
I know that barely scratches the surface of what some more experienced vim/ruby devs have (including the one offs in the vim.rc file).
Is there a list somewhere (or could we create one) of a bunch of the standard vim configurations needed to make programming ruby (and rails) more fun? Is there a zip/tarball somewhere with a good base setup?
take a look at tim pope's repos on git hub. Many, many awesome vim plugins and extensions for working with ruby and rails
http://github.com/tpope
snipMate (GitHub repo) is highly recommended. It enables you to use TextMate-style snippets in Vim.
snipMate is not Ruby-specific: by default, it comes with one file containing Ruby-specific snippets. If you are going to work a lot on Ruby-based code (Rails, RSpec, Shoulda, and the like), it's probably better to use one of the available snippets' collections and customize it as you see fit rather than writing your own snippets from scratch.
There is a pretty nice setup for your VIM environment.
http://github.com/akitaonrails/vimfiles
Just follow the instructions and in a minute or two you will have everything ready for Ruby(on Rails) development.
As Jed has mentioned above - tpope plugin is a "must have" and it is part of the akita vimfiles.
I have this in my ~/.vimrc to quickly see the output of a file I'm working on:
map <Leader>r :w! <bar> !ruby %<CR>
For rails, two essential plugins are rails.vim and the NERD tree, for navigating the directory of the rails project you're working on.

rails ruby-prof and benchmark testing

I'm running Rails 2.2.2. I've read a few articles about ruby-prof and profiling a rails app. And I'm confused as to how things are really working.
I was originally using this tutorial
http://snippets.aktagon.com/snippets/255-How-to-profile-your-Rails-and-Ruby-applications-with-ruby-prof
to profile my app, and it works. This involves writing your own profile environment and running > RAILS_ENV=profiling ./script/performance/request -n 100 profiling/homepage.rb
So here's my confusion. Somehow, this runs ruby-prof and opens up all the stats etc, but I can't see anywhere where ruby-prof is ever actually called.
So then I read further, and it seems 2.2.2 has benchmarking/profiling built in. So I write a test file in the performance section like so
require 'test_helper'
require 'performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionController::PerformanceTest
def test_worksheet
get '/reduction/worksheet'
end
end
and run
rake test:profile
Is this equivalent to what I was doing above, but just now it's integrated into the whole rails framework?
My next question is this. The original script ouput a flat file and html file, but I couldn't figure out how to also get a tree file automatically to open up with KCacheGrind, or in my case MacCallGrind. Can I add formats into my script call?
Edit: running the scripts through rake test seem to actually produce a tree file, great. Mac CallGrind however seems to hang when trying to parse it. Anyone know other tools for viewing these tree files?
Turns out, it's exactly the same, I must have been reading an old tutorial. All the benchmarking and profiling is now in Rails which is awesome. And it outputs txt,html and tree files, also awesome. Now if Only MacCallGrind would work, there's no way I'm installing the 4gb of KDE packages needed just to view a tree file.
Brad, you should contact the author of MacCallGrind and see if there's an update, or whether he/she has a fix for your files.
Oh that's right, that's me. Please email at richard at symbol aggmedia.net, as there is an update about to come out, and I'd love to have Ruby/Rails support available.

Resources