Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action. I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails. If you are interested in more, see my other Ruby posts.
Optional parentheses on method calls are such simple things, but they render some pretty code. Ruby allows you to omit parentheses on method calls in a lot of cases. Take the following code:
The two lines are equivalent, yet the first is just cleaner, don’t you think? Being a diehard C# guy, I do like parentheses, curly braces, and semi-colons, but I’ve got to say Ruby does yield some beautiful code.
In a recent Rails app I used Ryan Bates’ CanCan gem for authorization. It’s a really nice gem and allows you to define abilities for a user elegantly. Using CanCan, you can see how nice and clean the code looks. For example:
The code flows nicely; what can an admin do? “can manage all.”
If we were required to add parentheses it’s just is a bunch of extra, unneeded noise, and it breaks up the flow:
<gag> right?() :)