2 Ruby Fundamentals Module2 Slides

32
Classes and Objects Ruby Fundamentals

description

ruby2

Transcript of 2 Ruby Fundamentals Module2 Slides

Page 1: 2 Ruby Fundamentals Module2 Slides

Classes and Objects

Ruby Fundamentals

Page 2: 2 Ruby Fundamentals Module2 Slides

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

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

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

Instance Variables and Methods

class Spaceshipdef launch(destination)# go towards destination

endend

Page 6: 2 Ruby Fundamentals Module2 Slides

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

Accessors

class Spaceshipattr_accessor :destination

end

ship = Spaceship.newship.destination = "Earth"puts ship.destination

Page 8: 2 Ruby Fundamentals Module2 Slides

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

Accessors

class Spaceshipattr_accessor :destination, :name

end

Page 10: 2 Ruby Fundamentals Module2 Slides

Accessors

class Spaceshipattr_accessor :destination, :name

def cancel_launchdestination = "" # creates local variableself.destination = ""

endend

Page 11: 2 Ruby Fundamentals Module2 Slides

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

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

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

Inheritance

Spaceship

Rocket

Spaceship

BlankSlate

BasicObject

Object

Page 15: 2 Ruby Fundamentals Module2 Slides

Inheritance

Spaceship

Rocket

Object

ship = Spaceship.newship.clone

Page 16: 2 Ruby Fundamentals Module2 Slides

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

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

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

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

Class Variables

class Spaceship@@thruster_count = 2

def self.thruster_count@@thruster_count

endend

Page 21: 2 Ruby Fundamentals Module2 Slides

Class Instance Variables

class Spaceship@thruster_count = 2

def self.thruster_count@thruster_count

endend

Page 22: 2 Ruby Fundamentals Module2 Slides

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

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

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

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

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

Executable Class Bodies

Page 28: 2 Ruby Fundamentals Module2 Slides

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

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

Monkey Patching

Adding or modifying behavior at runtime – particularly 3rd party code

Page 31: 2 Ruby Fundamentals Module2 Slides

Equality

Page 32: 2 Ruby Fundamentals Module2 Slides

Summary

Objects and classes

Variables and methods in classes

Inheritance and method visibility

self, open classes, monkey patching and object equality