An Introduction to Object-Oriented Programming with Ruby

Nishant Mishra
Mar 15, 2018 · 8 min read

Getting a sense of the Object-Oriented Paradigm (OOP)

A Quick Syntax Lesson

class Car
def initialize(name, color)
@name = name
@color = color
end
def get_info
"Name: #{@name}, and Color: #{@color}"
end
end
my_car = Car.new("Fiat", "Red")
puts my_car.get_info

Accessing Data

class Car
def initialize(name, color) # "Constructor"
@name = name
@color = color
end
def color
@color
end
def color= (new_color)
@color = new_color
end
end
my_car = Car.new("Fiat", "Red")
puts my_car.color # Red
my_car.color = "White"
puts my_car.color # White

The easier way

class Car
attr_accessor :name, :color
end
car1 = Car.new
puts car1.name # => nil
car1.name = "Suzuki"
car1.color = "Gray"
puts car1.color # => Gray
car1.name = "Fiat"
puts car1.name # => Fiat

Talking about best practices

class Car
attr_accessor :name, :color

def initialize(name, color)
@name = name
@color = color
end
end
car1 = Car.new("Suzuki", "Gray")

puts car1.color # => Gray
car1.name = "Fiat"
puts car1.name # => Fiat

Class Methods and Class Variables

Inside the class definition

class MathFunctions
def self.two_times(num)
num * 2
end
end
# No instance created
puts MathFunctions.two_times(10) # => 20
class MathFunctions
class << self
def two_times(num)
num * 2
end
end
end
# No instance created
puts MathFunctions.two_times(10) # => 20

Outside the class definition

class MathFunctions
end
def MathFunctions.two_times(num)
num * 2
end
# No instance created
puts MathFunctions.two_times(10) # => 20

Class Inheritance

class Car
def to_s
"Car"
end
def speed
"Top speed 100"
end
end
class SuperCar < Car
def speed # Override
"Top speed 200"
end
end
car = Car.new
fast_car = SuperCar.new
puts "#{car}1 #{car.speed}" # => Car1 Top speed 100
puts "#{fast_car}2 #{fast_car.speed}" # => Car2 Top speed 200

Modules in Ruby

Modules as Namespace

module Patterns
class Match
attr_accessor :matched
end
end
module Sports
class Match
attr_accessor :score
end
end
match1 = Patterns::Match.new
match1.matched = "true"
match2 = Sports::Match.new
match2.score = 210

Modules as Mix-in

module PrintName
attr_accessor :name
def print_it
puts "Name: #{@name}"
end
end
class Person
include PrintName
end
class Organization
include PrintName
end
person = Person.new
person.name = "Nishant"
puts person.print_it # => Name: Nishant
organization = Organization.new
organization.name = "freeCodeCamp"
puts organization.print_it # => Name: freeCodeCamp

Scope in Ruby

Scope of variables

name = "Nishant"class MyClass
def my_fun
name = "John"
puts name # => John
end
puts name # => Nishant

Scope of constants

module MyModule
PI = 3.14
class MyClass
def value_of_pi
puts PI # => 3.14
PI = "3.144444"
puts PI # => 3.144444
end
end
puts PI # => 3.14
end

Scope of blocks

class BankAccount
attr_accessor :id, :amount
def initialize(id, amount)
@id = id
@amount = amount
end
end
acct1 = BankAccount.new(213, 300)
acct2 = BankAccount.new(22, 100)
acct3 = BankAccount.new(222, 500)
accts = [acct1, acct2, acct3]total_sum = 0
accts.each do |eachAcct|
total_sum = total_sum + eachAcct.amount
end
puts total_sum # => 900

Access Control

class Car
def initialize(speed, fuel_eco)
@rating = speed * comfort
end
def rating
@rating
end
end
puts Car.new(100, 5).rating # => 500

Example of the first way:

class MyClass
private
def func1
"private"
end
protected
def func2
"protected"
end
public
def func3
"Public"
end
end

Example of the second way:

class MyClass
def func1
"private"
end
def func2
"protected"
end
def func3
"Public"
end
private :func1
protected :func2
public :func3
end

Conclusion

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Nishant Mishra

Written by

An engineer by education and a developer by heart. Nishant is a budding developer from India. He enjoys problem solving, learning & creating awesome apps.

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead