2 ruby fundamentals module2 slides
DESCRIPTION
ruby2TRANSCRIPT
![Page 1: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/1.jpg)
Classes and Objects
Ruby Fundamentals
![Page 2: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/2.jpg)
Overview
Create classes and instantiate objects
Add instance variables and methods to your classes
Control the visibility of these variables and methods
Set initial state of the objects
Create class variables and methods
Leverage inheritance to re-use functionality between classes
self, current context, executable class bodies and object equality
![Page 3: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/3.jpg)
Creating Classes, Instantiating Objects
Class names start with a capital letter and use CamelCase
Capitalize abbreviations: XMLParser, JSONRequest
class Spaceshipend
ship = Spaceship.new
![Page 4: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/4.jpg)
Objects vs. Variables
“abc”
a = "abc" b = a
def method(arg) arg
end
method(b)
b = a.clone
“abc”“abc”
![Page 5: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/5.jpg)
Instance Variables and Methods
class Spaceshipdef launch(destination)# go towards destination
endend
![Page 6: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/6.jpg)
Instance Variables and Methods
inspect and p methods allow you to take a look inside objects
Instance variables are private while methods are public by default
class Spaceshipdef launch(destination)@destination = destination# go towards destination
endend
![Page 7: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/7.jpg)
Accessors
class Spaceshipattr_accessor :destination
end
ship = Spaceship.newship.destination = "Earth"puts ship.destination
![Page 8: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/8.jpg)
class Spaceshipattr_accessor :destinationattr_reader :nameattr_writer :name
end
ship = Spaceship.newship.name = "Dreadnought"puts ship.name
Accessors
![Page 9: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/9.jpg)
Accessors
class Spaceshipattr_accessor :destination, :name
end
![Page 10: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/10.jpg)
Accessors
class Spaceshipattr_accessor :destination, :name
def cancel_launchdestination = "" # creates local variableself.destination = ""
endend
![Page 11: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/11.jpg)
Virtual Attributes
class Spaceshipattr_accessor :destination
end
class Spaceshipdef destination@destination
end
def destination=(new_destination) @destination = new_destination
endend
![Page 12: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/12.jpg)
Virtual Attributes
class Spaceshipdef [email protected]
end
def destination=(new_destination)@autopilot.destination = new_destination
endend
ship = Spaceship.newship.destination = "Earth"puts ship.destination # outputs Earth
![Page 13: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/13.jpg)
Initialization
class Spaceshipdef initialize(name, cargo_module_count) @name = name@cargo_hold = CargoHold.new(cargo_module_count) @power_level = 100
endend
ship = Spaceship.new("Dreadnought", 4)
initialize("Dreadnought", 4)
![Page 14: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/14.jpg)
Inheritance
Spaceship
Rocket
Spaceship
BlankSlate
BasicObject
Object
![Page 15: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/15.jpg)
Inheritance
Spaceship
Rocket
Object
ship = Spaceship.newship.clone
![Page 16: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/16.jpg)
Inheritance
class Probedef deploy# deploy the probe
enddef take_sample# do generic sampling
endend
class MineralProbe < Probedef take_sample# take a mineral sample
endend
class AtmosphericProbe < Probedef take_sample# take a sample of the # atmosphere
endend
![Page 17: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/17.jpg)
Inheritance
Inheritance is for reusing functionality, not enforcing interfaces
class Probedef dock# probe specific# docking actions
endend
class Landerdef dock# lander specific# docking actions
endend
< Dockable < Dockable
class Dockabledef dock# implemented by subclasses
endend
![Page 18: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/18.jpg)
Inheritance
class Spaceshipdef capture(unit) unit.dock # works on anything with dock methodtransport_to_storage(unit)
endend
ship.capture(probe) ship.capture(lander)
![Page 19: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/19.jpg)
Class Methods and Class Variables
class Spaceshipdef self.thruster_count2
endend
Spaceship.thruster_count
ship = Spaceship.newship.thruster_count # this doesn't work
![Page 20: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/20.jpg)
Class Variables
class Spaceship@@thruster_count = 2
def self.thruster_count@@thruster_count
endend
![Page 21: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/21.jpg)
Class Instance Variables
class Spaceship@thruster_count = 2
def self.thruster_count@thruster_count
endend
![Page 22: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/22.jpg)
Method Visibility
class Spaceshipdef launchbatten_hatches# do other fun launch activities
end
def batten_hatchesputs "Batten the hatches!"
endprivate :batten_hatches
end
![Page 23: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/23.jpg)
Method Visibility
class Spaceshipdef launchbatten_hatcheslight_seatbelt_sign# do other fun launch activities
end
private
def batten_hatchesputs "Batten the hatches!"
end
def light_seatbelt_signputs "The seatbelt sign is now on."
endend
![Page 24: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/24.jpg)
Method Visibility
class Spaceshipdef launchbatten_hatcheslight_seatbelt_sign# do other fun launch activities
end
def batten_hatchesputs "Batten the hatches!"
end
def light_seatbelt_signputs "The seatbelt sign is now on."
end
private :batten_hatches, :light_seatbelt_signend
![Page 25: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/25.jpg)
Method Visibility
class Spaceshipdef self.disable_engine_containmemnt# dangerous – should be private!
end
# no error but does nothingprivate :disable_engine_containment
# this is the correct wayprivate_class_method :disable_engine_containment
end
![Page 26: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/26.jpg)
public is the default
private means “can’t be called with an explicit receiver”
private_class_method is private for class methods
protected means “allow access for other objects of the same class”
private and protected not used a whole lot
Method Visibility
![Page 27: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/27.jpg)
Executable Class Bodies
![Page 28: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/28.jpg)
self
class Spaceship
def self.thruster_count2
end
end
self == Spaceship, hence we’re adding this method to the class
def cancel_launch
self.destination = ""
seatbelt_sign(:off)
end
ship.cancel_launch
self == ship inside method
No explicit object reference, so seatbelt_sign also called on ship
![Page 29: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/29.jpg)
Open Classes
class Spaceshipdef batten_hatchesputs "Batten the hatches!"
endend
ship = Spaceship.new
class Spaceshipdef launchbatten_hatches# do other fun launch activitiesputs "Launched!"
endend
ship.launch
![Page 30: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/30.jpg)
Monkey Patching
Adding or modifying behavior at runtime – particularly 3rd party code
![Page 31: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/31.jpg)
Equality
![Page 32: 2 Ruby Fundamentals Module2 Slides](https://reader031.vdocuments.net/reader031/viewer/2022020102/55cf8f24550346703b9951e2/html5/thumbnails/32.jpg)
Summary
Objects and classes
Variables and methods in classes
Inheritance and method visibility
self, open classes, monkey patching and object equality