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.
Naming conventions can be a good thing providing there is actually some rhyme and reason to them. We’ve seen a lot of changes with conventions, like Hungarian Notation for example, who’s still using that? Within the context of .NET, there are defined conventions by Microsoft to which I would say I mostly follow (I still usually prefix private global member variables with an underscore). The guidelines for .NET are really good, much better than something very arbitrary like Hungarian Notation where no two developers use the same syntax on things.
There are two method naming conventions in Ruby that I really like. Within Ruby a method name can end in either a question mark (?) or an exclamation point (!). Having these conventions makes for nicer syntax. Let’s look at them in a little more depth.
The question mark on a method signifies that the method returns a boolean result. While I don’t believe there is a defined naming convention for .NET (I’ve seen different styles), within Ruby your method name doesn’t need to take into account something like a verb (e.g. IsAdmin, HasKey). Here are a couple of examples:
user.valid? user.active? user.admin? user.logged_in?
And thanks to the Ruby feature of optional parentheses for methods, your conditions look clean:
if user.logged_in? # Do something here end
The exclamation point at the end of a method signifies the method will modify the object that it is called on. Ruby calls these “dangerous” methods because they change the state of the object. A method without an exclamation point is considered a “safe” method because it makes a copy of the object and returns the copy instead of modifying the passed in object. Think of it like passing my reference vs. value. Since Ruby only has objects, there isn’t a concept of passing a value around. That said, the exclamation point is just a naming convention, so it’s up to you, the programmer, to use the convention when writing code.
Within the standard libraries, you’ll find a lot of places where there are multiple versions of the same method, one “safe” and one “dangerous.” One example would be some of the String methods within the standard Ruby String library. For example:
# Given a string greeting greeting = "hello" # upcase - converts a string to all upper case puts greeting.upcase #=> "HELLO" puts greeting #=> "hello" (greeting was not modified by .upcase) puts greeting.upcase! #=> "HELLO" puts greeting #=> "HELLO" (greeting was modified by .upcase!)
There are times where the convention of changing the state of an object doesn’t always apply, but the method is “dangerous.” An example of this can be found within Rails’ ActiveRecord. You will find two methods, save and a save!.** Both versions save a model, however with **save!, validations always run, and if any of them fail, an ActiveRecord::RecordInvalid error is raised, whereas the “safe” method, save, only returns true or false if there are validation errors.