Main Content

Ruby objects - a primer

Archive - Originally posted on "The Horse's Mouth" - 2010-01-29 04:28:28 - Graham Ellis

If you're new to Ruby, but familiar with Object Oriented Programming, you might like to take a quick look at the example that I wrote during yesterday's course which shows how all the major OO elements are implemented in Ruby. The sample source is [here].

Here are some of the key facts:

• Classes are defined with a class statement, and the class name must start with a capital letter. The end of the class definition is an end statement - example:

class Shape

  [snip]

  end


• Classes that inherit from other classes use a < notation - example:

class Box < Shape

  [snip]

  end



• Methods within classes are defined using the def keyword, once again through to an end

def bright()

  return (@red + @green + @blue) / 768.0

  end



• The constructor is a method called initialize

def initialize(red,green,blue)

  @red=red

  @green=green

  @blue=blue

  end



• You may call up the contructor of your parent class with a super call.

def initialize(red,green,blue,x,y)

  super(255-red,green,blue)

  @x = x

  @y = y

  end



• You do NOT give the object a name in your parameter list in Ruby - if you need to make an explicit reference to the current object, you use self which is a reserved word.

def lesser (second)

  if self.area < second.area

    return self

  end

  return second

  end



• All variables are created / typed / sized as the program runs - there is no need for you to declare them.

andsofourth = Shape.build(4)

glug = [brown, purple, green, andsoon]



• You refer to a dynamic (object) variable with a leading @ character, and to a static (class) variable with @@. Variables that start with $ are global, and variables that do not have any lead character are local.

No examples of @@ and $ in my program - use them sparingly



• You can define multiple classes in a single file, and indeed I recommend that you keep groups of classes which will be maintained and distributed together to cut down the number of files, file management time, etc.

• You can (should!) add a test program onto the end of a file that defines class(es) by embedding the test code into a condition which checks if it's being run direct from the command line.

if __FILE__ == %body%

  [snip]

  end



• You call up the constructor method of a class using the new method on the class.

purple = Circle.new(100,40,200,60)



• Methods are called using a dot notation. There is no need to follow a method call with a pair of brackets if you are not passing in any parameters.

print "#{current.bright} - #{current.sides} - #{current.area} "

puts current.class



• You may define a method within the module / namespace of a class also using the dot notation. This is a static method and is useful for (for example) factory methods which return newly constructed objects who's type may differ depending on the input data

def Shape.build(value)

  if value > 5

    return Box.new(50,100,150,140,140)

  end

  return Circle.new(150,150,150,12)

  end



• The ruby interpretter stops at __END__ so you may park documentation and sample results and other comments at the end of your program

__END__

 

Dorothy-2:rubob grahamellis$ ruby shapes.rb

0.234375 - 4 - 1250 Box

0.442708333333333 - 1 - 2827.433385 Upper

0.364583333333333 - 1 - 2375.8294415625 Upper

0.592447916666667 - 4 - 19600 Box

0.5859375 - 1 - 113.0973354 Upper

Largest area is 19600

Dorothy-2:rubob grahamellis$



I hope that's a quick / useful reminder or introduction to Object Orinetation in Ruby. It assumes prior OO and Ruby knowledge - if you need to learn Ruby, or more about Ruby, please look at our training courses - we offer a Learning to program in Ruby course for delegates who have never programmed before, and a Ruby Programming course for delegates who have programmed before, but not in Ruby.

Both courses include an overview of Object Oriented Principles so that you'll come across words like class and object and instance and learn what they mean, as well as going on to talk about methods, interitance, polymorphism, overriding and much more. I'll also run a session that overviews the principles of object oriented design - how to come up with a scheme that works and is efficient and maintainable, and I'll point to to techniques such as extreme programming and UML, encouraging you to follow some of their principles where it's appropriate to you.