In the first part, I’m just going to be covering the basics.
Conventions
Before we start, some coding conventions. Firstly, CamelCase for methods and variable names is out – for Ruby it’s lowercase and underscores all the way – it_is_this_way, itIsNotThisWay. Classes and Modules (of which more later) should start with an uppercase letter, but the files can start with a lowercase letter. The only break with this convention is in terms of class and module names – here you camel case should be used. Of course you can use CamelCase everywhere if you want, but it’ll be confusing when calling Ruby code written by other people.
Syntax
Some things you’ll notice straight off – brackets for methods with parameters are optional, so do_stuff(10, 20, 30) works, as does do_stuff 10, 20, 30. The current recommendation is to use paranthesis when you have arguments as there is a chance that Ruby 2 might break some method calls without brackets (I’d appreciate some clarification on this!).
Methods and if blocks use end to terminate rather than brackets:
[ruby]
def do_stuff
if something
…
elsif something_else
…
end
end
[/ruby]
Classes
Classes in Ruby are similar to classes in Java (we’ll deal with typing later). For example to define a Polygon class, you do the following:
[ruby]
class Polygon
…
end
[/ruby]
You can also subclass:
[ruby]
class Square < Polygon
[/ruby]
You implement constructors by defining an initialize method:
[ruby]
def initialize
…
end
[/ruby]
And call them using Polygon.new, Square.new etc.
Modules
You define modules using:
[ruby]
module Utils
…
end
[/ruby]
You use modules as mixins using the include keyword. For example imagine the following:
[ruby]
module Renderer
def draw_line(x,y)
…
end
end
[/ruby]
Your Square class can then include Renderer to use the draw_line method:
[ruby]
class Square < Polygon
include Renderer
end
[/ruby]
Some might argue you could get this using an Abstract base class – and you’d be right. However there are no limits to the number of mixins you can include, whereas you can only have one Abstract base class. Ruby provides several very useful mixins – for example if you define the = operator (we’ll be covering operators later), by including the comparable module you get <, <=, ==, >=, and > for free.
<h3 id="methods>Methods and visibility
As you’ve already seen, you define a method using def method_name – if you have parameters, you define it using def method_name(arg1,arg2)). Ruby does support optional parameters. In Java, you’ll often see code like this:
[java]
void doSomething(String name) {
doSomething(name, null);
}
void doSomething(String name, String description) {
…
}
[/java]
In Ruby you can do:
[ruby]
def do_something(name, description = nil)
…
end
[/ruby]
In Java, where you have protected, public, private and default visibility, you have protected, public, private in Ruby. private in Ruby is more like protected in Java however – it can be called from either the class it was defined in, or subclasses. private methods however can only be called when using self as the reciever, so if do_stuff is private:
[ruby]
self.do_stuff #Allowed
do_stuff #Not allowed
[/ruby]
Put more simply, a private method cannot be called in a static context.
Ruby’s protected methods can be called only in the class itsef or subclasses, but can be called without a reciever as well as with a reciever:
[ruby]
self.do_stuff #Allowed
do_stuff #Allowed
[/ruby]
You have two options when defining method visibility. You can use the visibility keywords to define private/protected/public regions:
[ruby]
protected
def this_is_protected
…
end
private
def this_is_a_private_method
…
end
def this_is_also_private
…
end
[/ruby]
You can also do it on a single line:
[ruby]
def this_is_protected
…
end
def this_is_a_private_method
…
end
def this_is_also_private
…
end
protected :this_is_protected
protected :this_is_a_private_method, :this_is_also_private
[/ruby]
Personally I prefer the first technique.
Next time we’ll be looking at operators, arrays, hashes and perhaps even the Ruby typing system.
- Update 1: Fixed wording in the modules section
- Update 2: Updated methods section thanks to feedback from Curt Hibbs
- Update 3: Fixed typos in the classes and methods sections (thanks Gavri Fernandez)
- Update 4: Fixed typos (thanks Eliot and Alex) and clarified use of camel case (thanks Jon ).
- Update 5 21st April 2006: Fixed up some missing text due to WordPress import. Used new syntax highlighting for code samples
