One thing I really hate about Windows development is no terminal. I love the command-line, and while there exists one or two substitutes, it’s still a far cry from the real terminal. The Mac terminal. Despite my bitterness, most, if not all, of these tips concern the command-line.
Subversion integration
When I first started using subversion, rather than jumping right in with tortoise, it was recommended that I become familiar with the svn commands. Since I’m somewhat partial to the command-line, that’s what I did. In fact, that’s all I do.
One thing that quickly annoyed me was having to do an svn add whenever I generated something (model, controller, migration, etc.) That’s when I found use in tortoise, to easily view all non-versioned files. Then I learned that you can pass the --svn option onto the end of any “generator” to automatically do an svn add on those new files. Oh, and it works for script/destroy, too. Sweet.
1 2 3 4 5 6 7 8 9 10 11 | ## will also add files to subversion $ ruby script/generate model Project --svn ## will also remove files from subversion $ ruby script/destroy model Project --svn ## FILES AFFECTED: # 1) project.rb - the model itself (in app/models) # 2) project_test.rb - unit test (in test/unit) # 3) projects.yml - project fixture (in test/fixtures) # 4) XXX_create_projects.rb - migration (in db/migrate) |
It works with all generators, including your own.
Console: interactive application
The console is a good place to quickly interact with your application. In fact, did you know your entire application is actually tucked away in an object, waiting to be woken up from the console? Here’s some of the things you can do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ ruby script/console >> app.url_for :controller => :public, :action => :about => "http://example.com/public/about" >> app.get("/public") => 200 >> app.get("/ryan") => 404 # others include >> app.response.headers >> app.response.body[0,75] >> app.post("/collection/add/1") >> app.response.redirected_to >> app.follow_redirect! # fix errors in your code, then... >> reload! |
Pretty straight-forward.
Console: setting a default object
If you’re going to be interfacing with your “app” object a lot (or any object for that matter), it may be worth it to set it as the default.
1 2 3 4 5 6 7 8 9 | >> irb app >> get "/public" => 200 >> get "/ryan" => 404 >> exit # now you're back to normal >> |
Console: routing
Routing can get tricky, and sometimes you need to stop and quickly double check that it’s all working properly. Well, by setting the default object to the Routes class, you can quickly analyze your routes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >> irb ActionController::Routing::Routes >> puts routes ANY / {:action=>"index", :controller=>"public"} ... ANY /:controller/:action/:id.:format/ {} # independently play with paths >> recognize_path "/about" => {:controller=>"public", :action=>"about"} >> recognize_path "/public" => {:controller=>"public", :action=>"index"} # or check a path inversely >> generate :controller => "public", :action => "about" => "/about" >> generate :controller => "public", :action => "index" => "/" |
And on and on. The console is awesome, really, and I don’t use it near as much as I should. I just wish I had that Mac terminal.
01
Chris on Wed Mar 28 at 03:42PM
One thing to note: -c is the same as—svn (save yourself a few keystrokes!)
Also, I wish IRB could output colors like your examples. The closest thing I’ve found (just now) is Wirble, but the terminal is limited to ANSI colors.
02
Ryan on Wed Mar 28 at 04:40PM
I’m all about saving the keystrokes, thanks. Yeah, colorful outputs would be wonderful. Add up all of those extra seconds it takes to decipher the output for one year, and it’s probably a full work day wasted. Or not, but still.