Naming conventions in Ruby on rails

By: Brian Marick Viewed: 153167 times    

A Ruby name may contain letters, numbers, and the underscore character (not a hyphen). Names can’t begin with a number, nor may they include spaces. Case matters: my_ship is not the same name as my_Ship.

When a name begins with a capital letter, you’re telling Ruby that you expect it always to refer to the same object. Ruby will complain if you try to use the same name for a different object:

irb(main):007:0> MyShip = "a cutter"

=> "a cutter"

irb(main):008:0> MyShip = "a bark"

(irb):4: warning: already initialized constant MyShip

=> "a bark"

(Ruby complains but still obeys.)

When a multiword name begins with a lowercase letter, it’s conventional to separate the words with underscores, like my_fine_name. When one begins with a capital letter, the convention is to capitalize each word:

MyFineName. I don’t know the rationale behind the difference. As a special case, message names can end with a question mark or an exclamation point. When one ends in a question mark, it’s a signal that the message asks a true/false question of its receiver. An exclamation point is a signal to the reader that the message does something special and perhaps unexpected.

Most Viewed Articles (in Ruby )

Latest Articles (in Ruby)

Comment on this tutorial