Jun
20
Rails Continues To Add Polish
Filed Under ruby, technology |
I don’t really have the stones to run “edge” rails, but I try to keep up to date with all of the stable releases wherever it makes sense to. As many know, Rails 2.1 was just released with a host of new, severely awesome, features (like named_scopes, better support for third-party VMs, etc.), but edge already has some intereging functionality that makes me look forward to 2.2. Here is some of the highlights that interest me:
#link_to has new block functionality
Say you had a block of HTML you wanted to wrap an anchor around. there wasn’t a very acceptable way to do this using link_to, you’d have to do something like this to utilize it:
1 2 3 4 | <a href="<%= url_for :controller => 'users', :action => 'view', :id => @user %>"> <strong>Click this to view the user</strong> <em>this is more text that I want to be in the link</em> </a> |
I absolutely hate the idea of having brackets inside of brackets. That just looks, hell it smells wrong! This feature is awesome because it prevents you from writing REALLY ugly code! Now you can just do this:
1 2 3 4 | <%- link_to(:controller => "user", :action => "view", :id => @user) do %> <strong>view this user</strong> <em>more text here</em> <%-end%> |
That to me feels much more correct! Thanks to Sam Stephenson + DHH for this one.
String Inquirer
At first, I didn’t see much value to this feature, but after thinking about it for a bit, I can see its usefulness. Take a look at the example:
1 2 | str = ActiveSupport::StringInquirer("awesome") str.awesome? #=> true |
Basically that new function saves you from manually writing this:
1 2 | str = "awesome" str == "awesome" ? true : false |
Definitely a nice enhancement to the code, makes it much more readable. But, I don’t love the way you have to instantiate it, directly touching ActiveSupport that is. I’d prefer a syntax like “my string”.inquirize or something.
Along the lines of prettier code, we’ve also got the new Array#present? functionality which basically is a cleaner way of saying !Object#blank?
Declarative Block Syntax For Unit Testing
This is a very nice addition to the Test::Unit framework. A lot of people prefer RSpec over Test::Unit, but I prefer how close to the xUnit framework the syntax is. Anyhow, take a look at the old way to write unit test functions:
1 2 3 | def test_should_verify_user_logged_in assert_equal @user.is_logged_in?, true end |
And now the new way to do things:
1 2 3 | test "should verify user is logged in" do assert_equal @user.is_logged_in?, true end |
Its a subtle change, but, it makes the tests far more readable. I also like the move to a more DSL-esque syntax, rather than defining a bunch of functions.
All in all, the next Rails release continues to impress!
Comments
2 Comments so far
str == “awesome” ? true : false
doesn’t the line (str == “awesome”)
return true or false without the ternary operator?
I can possibly see the utility of StringInquirer, but the instantiation looks abominable.
@p-daddy, you are correct. You could just do that as well. As I said, I hope they also change the instantiation as well.