Ruby - a pure object oriented language

CioataPetru 412 views 19 slides Jun 29, 2017
Slide 1
Slide 1 of 19
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

About This Presentation

Codecamp Suceava - 24 June 2017
What is an object-oriented language? Why is Ruby considered an OOP language and which are its main features?
An overview of meta programming.


Slide Content

Petru CIOATĂ
Ruby - a pure
object oriented
language

Petru CIOATĂ
Summary
●Ruby Intro
●What is an OOP language?
●Everything is an object ...
●Class vs Instance
●Inheritance. Modules and Mixins
●Metaprogramming overview
●Metaprogramming DEMO
●Q&A

Petru CIOATĂ
Ruby is ...
“A dynamic, open source programming language with a focus on simplicity and
productivity. It has an elegant syntax that is natural to read and easy to write.”

Petru CIOATĂ
Ruby is ...

●Dynamic
●Reflective
●Object-Oriented
●General-Purpose
●Dynamically typing

Petru CIOATĂ

Petru CIOATĂ
What is an OOP language?
A brief OOP History
The main OOP Concepts:
●Abstraction
●Encapsulation
●Inheritance
●Polymorphism

Petru CIOATĂ
Everything is an object ...
●-24.abs
●112 .send(:+, 8)
●true.class
●nil.equal?(false)
●“hello”.reverse
●(1..10).to_a
●[1, 2, 3].reverse!
●Class.class

Petru CIOATĂ
Everything is an object ...


If it walks like a duck and quaks like a duck, I would call it a duck

Petru CIOATĂ
Class vs Instance


●Instance
●Instance variables
●Instance methods


class Presentation
# class variables
@@total_no = 0
# constructor
def initialize(
title, author, track_no, duration)
@@total_no += 1
# instance variables
@title = title
@author = author
@track_no = track_no
@duration = duration
end
# instance methods
def to_s
"Welcome to #{@title} presented by
#{@author}"
end
end


ruby_oop = Presentation.new("Ruby OOP", "Petru", 1, 30)
puts ruby_oop.to_s # Welcome to Ruby-OOP presented by Petru

ruby_oop.instance_variables # [:@title, :@author, :@track_no, :@duration]

Presentation.instance_methods(false) # [:to_s]

Petru CIOATĂ
Class vs Instance


●Class
●Class Variables
●Class Methods

class Presentation
# class methods
def self.no_of_presentations
@@total_no
end

def Presentation.no_of_presentations
@@total_no
end

class << self
def no_of_presentations
@@total_no
end

def total_duration(*presentations)
presentations.collect(&:duration)
.inject(0, &:+)
end
end
end

ruby_oop = Presentation.new( "Ruby a pure OOP language", "Petru", 1, 30)
vue_js = Presentation.new( "Vue JS Intro", "Dan Ichim", 1, 40)

puts Presentation.no_of_presentations
puts Presentation.total_duration( ruby_oop, vue_js)

Presentation.methods(false) # [:no_of_presentations, :total_duration]
Presentation.class_variables(false) # [:total_no]

Petru CIOATĂ
Class as Instance

#### class as Object
Presentation = Class.new do
@@total_no = 0

def initialize(title, author, track_no,
duration)
@title = title
@author = author
@track_no = track_no
@duration = duration
@@total_no += 1
end

def to_s
"Welcome to #{@title} presented by
#{@author}"
end
end

ruby_oop = Presentation.new("Ruby a pure OOP language", "Petru", 1, 30)
puts ruby_oop.to_s

ruby_oop.instance_variables # [:@title, :@author, :@track_no, :@duration]

Presentation.instance_methods(false) # [:to_s]


Presentation.superclass
Class.superclass

Petru CIOATĂ
Inheritance. Modules and mixins
class Shape
attr_accessor :name
def initialize(name)
@name = name
end

def area
puts "TBI"
end

def perimeter
puts "TBI"
end
end

class Square < Shape
attr_accessor :side_length

def initialize(name,
side_length)
@side_length = side_length
super(name)
end

def area
4 * self.side_length
end

def perimeter
self.side_length ** 2
end
end
s = Square.new("patratel", 1.5)
puts s.to_s
puts s.area
puts s.perimeter

●Single inheritance
●Accessor and attributes
○attr_reader
○attr_writer
○attr_accessor

Petru CIOATĂ
Inheritance. Modules and mixins
module HelperMethods
def characteristic(*attributes)
attributes.inject(desc = "Shape :"){|attribute| desc << " #{attribute}"}
end

def hello_there
p "Hello from module”
end
end

class Square < Shape
include HelperMethods
end

s = Square.new("patratel", 1.5)
puts s.characteristics(s.name, s.side_length)
s.hello_there

class Square < Shape
extend HelperMethods
end

Square.characteristics
Square.hello_there

Petru CIOATĂ
Inheritance. Modules and mixins

Petru CIOATĂ
Metaprogramming overview
●Writing code that manipulates language constructs at runtime

Petru CIOATĂ
Metaprogramming DEMO
●Open class
●Monkey Patching
●method_missing
●define_method
●class_eval
●instance_eval

Petru CIOATĂ
Books
https://www.amazon.com/dp/0596516177
https://www.amazon.com/dp/0321721330/
https://www.amazon.com/dp/B00N9I0RMQ/

Petru CIOATĂ

Petru CIOATĂ