O'Reilly logo

Learning Sinatra by Manoj Sehrawat, Sudeep Agarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Classes and objects

Ruby is a multi-paradigm language and it supports object-oriented paradigms like any other OO language does. We can have modules, classes, methods, objects, inheritance, and so on.

Defining modules and classes

Like any other object-oriented language, we can define modules and classes in Ruby. A module is generally used to encapsulate one or more classes and improve readability. In the next few chapters, we will see how defining modules can improve the quality of the code:

module <module_name>
  class <class_name>
  end
end

For example, let's see the following code:

module Vehicle
  class Car
  end
end

Constructors

We can have constructor methods for the Car class:

class Car def initialize(manufacturer, cost) @manufacturer = manufacturer @cost ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required