Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

RB
Ruby
0/12 completed
Beginner
Hello RubyVariables and typesConditions and loopsMethods
Intermediate
Arrays and hashesBlocks, procs and iteratorsClasses and objectsModules and mixins
Advanced
Exceptions and filesMetaprogramming
Pro
Gems, Bundler and testingRuby on Rails basics
Tracks / Ruby / Intermediate

Classes and objects

Ruby is object-oriented through and through: every value is an object, and every object belongs to a class. Defining your own classes lets you group data with the methods that work on it, like a BankAccount that knows how to deposit and withdraw.

Defining a class

Ruby · editable
  • Dog.new(…) creates an object and calls initialize with the arguments.
  • Instance variables (@name) belong to each object and are private to it.
  • Class names are CamelCase and are constants.

Attributes: getters and setters

Instance variables can't be read from outside. Expose them with accessor methods, and Ruby generates them for you:

Ruby · editable
Macro Generates
attr_reader :x def x = @x
attr_writer :x def x=(v) = @x = v
attr_accessor :x Both

kb.stock -= 3 calls the stock getter and then the stock= setter: in Ruby, even assignment to an attribute is a method call.

to_s, inspect and comparing objects

Ruby · editable
  • to_s is used by puts and interpolation; inspect by p.
  • Operators are just methods: define +, == or <=> and they work.
  • Define <=> and include Comparable to get <, >, between?, clamp, sorting and max for free.

Class methods and class-level data

Methods defined with self. belong to the class itself, not to each object. They're used for constructors and helpers:

Ruby · editable

Inheritance

A class can inherit from another and add or change behaviour:

Ruby · editable

super calls the same method in the parent class. Ruby has single inheritance (one parent). Sharing behaviour between unrelated classes is done with modules, in the next lesson.

Public, private and protected

Ruby · editable
  • public (the default): callable by anyone.
  • private: only callable from inside the object, without an explicit receiver.
  • protected: callable by other objects of the same class, which is useful for comparisons.

Data classes: Struct and Data

For simple value objects, skip the boilerplate:

Ruby · editable

Data (Ruby 3.2+) is ideal for small immutable values like coordinates, money or API results.

Practice

  1. Write a BankAccount class with deposit, withdraw (refusing overdrafts) and a balance reader.
  2. Give it a to_s that prints like Asha: ₹1,250.00.
  3. Create SavingsAccount < BankAccount that adds interest with add_interest(rate).
  4. Define a Temperature class with <=> and Comparable, and sort an array of them.
✎ Check your understanding

1.What does initialize do?

2.What does attr_accessor :age generate?

3.What does super do inside a method?

4.How do you make a method callable only from inside the object?

← Blocks, procs and iterators