views:

44

answers:

2

I've got a Ruby project started with NetBeans, so the Rake file has been generated. Is there a way that I can run the project over the command line?

It runs fine when I use F6 through NetBeans, as does my automated test suite with Alt+F6. I'm essentially looking for something like...

$ rake run

Does this exist?

+2  A: 

The goal of ruby programming is (generally) to either write a web application, or write a program that can be run from the command line.

For a web application a rake run option might be worthwhile, but really the most common web applicaition framework is Rails, and for rails, you can just run a dedicated webserver running your web app with script/server.

For a commandline program, just run whichever ruby file you have intended as the main file (the one with the code that runs at startup). Ruby doesn't have any of the difficulties that Java does (e.g. having a jar file with the right Main-class attribute, and getting the classpath right, etc...). So you don't really need a rake run target, because there's no complexity that needs to be hidden in the rakefile.

Ken Bloom
The idea makes sense, because its a command line program. But when I run ruby <Main Class>.rb, it raises an error because it cannot resolve the proper path for a class it needs to use. Why does the Command Line have a problem and NetBeans doesn't?
Mike
@Mike -- Are you using Ruby 1.9.2? If so, then your problem might be because it doesn't include the current directory in the path ruby uses to find ruby scripts. You can fix that by adding your project to the RUBYLIB environment variable.
Jeremy
Netbeans probably adds the project path to `$:` so that `require` can be resolved relative to it. This can generally be fixed by putting `$: << File.dirname(__FILE__)` in the file that you plan to run. Alternatively, use the Facets gem or Extensions gem, and you'll get `require_relative` that can fix this as well. (`require_relative` is builtin in Ruby 1.9.2)
Ken Bloom
@Jeremy: then would the problem be that Netbeans somehow sets `$:` before the script runs (and his code doesn't), or would it be that Netbeans uses Ruby 1.9.1 and outside of Netbeans it uses some other version of Ruby? (IIRC, 1.8 doesn't put `.` in `$:` either)
Ken Bloom
That's what I was implying, that Netbeans might modify the load path automatically when you run the application. Then again, I'm not sure if JRuby works differently.
Jeremy
@Jeremy: that was an either-or question, if you know the answer. Tell us as much as you know about what Netbeans is doing.
Ken Bloom
@Ken: My first sentences was for your first statement, and my second sentence was for your second statement. That's all I have. I was just giving ideas. If I knew more, I would have said more.
Jeremy
Thanks guys! Sorry for the late accept.
Mike
+2  A: 

Although Ken's right, you can certainly make a rake task to run your program. In lib/tasks/project.rake:

namespace :project do
    task :run do 
        call_your_code()
    end
end

and then rake project:run will do what you want.

Isaac Cambron