09. Ruby Object Oriented Programming - Ruby Core Teaching

quanhoangd129 161 views 31 slides Jul 19, 2024
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

Ruby OOP


Slide Content

RUBY OOP
Hoang Quan(BlazingRockStorm)

Contents
OOP
EXCEPTION
MODULE

OOP

What is Object
Oriented
Programming?
Object-oriented programming (OOP) is a
programming paradigm based on the
concept of objects, which can contain
data and code: data in the form of fields
(often known as attributes or properties),
and code in the form of procedures
(often known as methods). In OOP,
computer programs are designed by
making them out of objects that interact
with one another.
In Ruby, everything is an object!

In Ruby, everything is an
object!

Class

Class
definition

Initialize
Method

Instance
Variable

attr_reader,
attr_writer,
attr_accessor

attr_reader,
attr_writer,
attr_accessor

attr_reader,
attr_writer,
attr_accessor

Instance
method, class
method

Four Pillars of OOP

Four Pillars of
OOP
INHERITANCE ENCAPSULATION
POLYMORPHISM ABSTRACTION

Inheritance

Inheritance

Encapsulation

Encapsulation
In this case, the Dog class has an instance variable
@name, which is encapsulated within the class. It
can be accessed using the get_name method.

Encapsulation:
public,
protected and
private

Polymorphism

Polymorphism
In this case, both Dog and Cat
are subclasses of Animal and
redefine the speak method. This
is polymorphism in action.

Abstraction

Abstraction
Abstraction in Ruby can be implemented using
modules, which are like classes but cannot be
instantiated

Module

Module
In Ruby, modules are somewhat similar to classes: they are things
that hold methods, just like classes do. However, modules can not
be instantiated.
You can mix in more than one module in a class. However, a class
cannot inherit from more than one class.

Module: include

Module: extend

Exception

Exception
Exceptions are ways of dealing
with unexpected events.
Exceptions in Ruby are blocks
with structure begin/rescue
In Ruby, there is some pre-
defined Exception Object like:
Exception, NoMethodError,
ZeroDivisionError,...

Thank you!
ありがとうございます!