Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges....

75
Ruby Monstas Session 11

Transcript of Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges....

Page 1: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Ruby Monstas

Session 11

Page 2: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Agenda

● Recap / Questions● Classes / Objects and OOP● Exercises

Page 3: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Recap

Page 4: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

QuestionsBlocksSymbolsRanges

Page 5: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Object-oriented programming

Classes & Objects

Page 6: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cookie cutter analogy

Page 7: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cookie cutter analogy

Cookie cutterCookies

Page 8: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cookie cutter analogy

Cookie cutter→ Class

Cookies→ Objects

Page 9: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cookie cutter analogy

The cookie cutter (class) is used to make the dough (memory) into cookies (objects).

Page 10: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cat exampleThe domain of our application: cats!Therefore it makes sense to have a “Cat” class (cookie cutter).We can later use our “Cat” class to produce concrete (actual) cats.

Page 11: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cat example: Class diagram

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 12: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cat example: Class diagram

data

behaviour

class nameCat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 13: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cat example: InstantiationCat

name: “Sgt. Fuzzyboots”coat: “tabby”weight: 3.1

...

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 14: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cat example: InstantiationCat

name: “Sgt. Fuzzyboots”coat: “tabby”weight: 3.1

...

Cat

name: “Gorbypuff Thunderhorse II”coat: “cream”weight: 4.7

...

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 15: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cat example: InstantiationCat

name: “Sgt. Fuzzyboots”coat: “tabby”weight: 3.1

...

Cat

name: “Gorbypuff Thunderhorse II”coat: “cream”weight: 4.7

...

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 16: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Cat example: InstantiationCat

name: “Sgt. Fuzzyboots”coat: “tabby”weight: 3.1

...

Cat

name: “Gorbypuff Thunderhorse II”coat: “cream”weight: 4.7

...

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 17: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 18: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: initialize method

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 19: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: initialize method

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 20: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: initialize method

initialize is a special method, that allows us to initialize the object we’re creating with some data.The same concept is called “constructor” in other programming languages.

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

Cat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Page 21: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Using initializeCat

name: “Sgt. Fuzzyboots”coat: “tabby”weight: 3.1

...

Cat

name: “Gorbypuff Thunderhorse II”coat: “cream”weight: 4.7

...

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new( 'Sgt. Fuzzyboots', 'tabby', 3.1)

gorbypuff = Cat.new(

'Gorbypuff Thunderhorse II',

'cream',

4.7

)

Page 22: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Using initialize

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)

1. Cat.new is called, that means initialize gets called in the Cat class.

Page 23: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Using initialize

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)

2. Local variables name, coat and weight get assigned to the values passed.

Page 24: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Using initialize

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)

3. Instance variables @name, @coat and @weight get assigned to the values of the local variables name, coat and weight.

Page 25: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Using initialize

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)

4. initialize method is over. Ruby builds an instance (object) of type Cat that contains the data and returns it from the new method call.

Page 26: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Using initialize

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)

5. The returned object of type Cat gets assigned to a local variable called sgt_fuzzyboots outside the class.

Page 27: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

Page 28: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

Page 29: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12 puts aend

my_method

Page 30: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12 puts aend

my_method

12

Page 31: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12end

my_methodputs a

def my_method a = 12 puts aend

my_method

12

Page 32: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12end

my_methodputs a

NameError (undefined local variable or method `a' for main:Object)

def my_method a = 12 puts aend

my_method

12

Page 33: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12end

my_methodputs a

NameError (undefined local variable or method `a' for main:Object)

def my_method a = 12 puts aend

my_method

12

def my_method a = 12end

a = 23my_methodputs a

Page 34: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12end

my_methodputs a

NameError (undefined local variable or method `a' for main:Object)

def my_method a = 12 puts aend

my_method

12 23

def my_method a = 12end

a = 23my_methodputs a

Page 35: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12end

my_methodputs a

NameError (undefined local variable or method `a' for main:Object)

def my_method a = 12 puts aend

my_method

12 23

def my_method a = 12end

a = 23my_methodputs a

def my_method a = 12 puts aend

a = 23my_method

Page 36: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesLocal variables are useable from when they’re first defined until the end of the scope they’re defined in.a = 12

puts a

12

def my_method a = 12end

my_methodputs a

NameError (undefined local variable or method `a' for main:Object)

def my_method a = 12 puts aend

my_method

12 23

def my_method a = 12end

a = 23my_methodputs a

12

def my_method a = 12 puts aend

a = 23my_method

Page 37: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)

# do something else

Local vs. instance variables

Page 38: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat def initialize(name, coat, weight) @name = name @coat = coat @weight = weight endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)

# do something else

Local vs. instance variables

name, coat, weight are in scope here, but not outside initialize!

name, coat, weight are gone here!

Page 39: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesInstance variables are useable without definition (default value: nil). They are in scope inside an object. Each object has its own instance variables.class MyClass def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

Page 40: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesInstance variables are useable without definition (default value: nil). They are in scope inside an object. Each object has its own instance variables.class MyClass def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

(nothing (nil))

Page 41: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesInstance variables are useable without definition (default value: nil). They are in scope inside an object. Each object has its own instance variables.class MyClass def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

class MyClass def initialize @my_variable = 12 end

def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

(nothing (nil))

Page 42: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesInstance variables are useable without definition (default value: nil). They are in scope inside an object. Each object has its own instance variables.class MyClass def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

class MyClass def initialize @my_variable = 12 end

def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

(nothing (nil)) 12

Page 43: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesInstance variables are useable without definition (default value: nil). They are in scope inside an object. Each object has its own instance variables.class MyClass def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

class MyClass def initialize @my_variable = 12 end

def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

(nothing (nil)) 12

class MyClass def initialize(my_number) @my_variable = my_number end

def my_method @my_variable endend

a = MyClass.new(12)b = MyClass.new(23)

puts b.my_methodputs a.my_method

Page 44: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Local vs. instance variablesInstance variables are useable without definition (default value: nil). They are in scope inside an object. Each object has its own instance variables.class MyClass def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

class MyClass def initialize @my_variable = 12 end

def my_method @my_variable endend

my_object = MyClass.newputs my_object.my_method

(nothing (nil)) 12

class MyClass def initialize(my_number) @my_variable = my_number end

def my_method @my_variable endend

a = MyClass.new(12)b = MyClass.new(23)

puts b.my_methodputs a.my_method

2312

Page 45: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Behaviour

data

behaviour

class nameCat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Class diagram Ruby

?

?

Page 46: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Behaviour

data

behaviour

class nameCat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Class diagram Ruby

Instance variables

?

Page 47: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Classes in Ruby: Behaviour

data

behaviour

class nameCat

name: Stringcoat: Stringweight: Float

+move_to+location+speak+eat+information

Class diagram Ruby

Instance variables

Methods

Page 48: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: BehaviourWhat we want (our “contract” for the Cat class):

Meow!the bedSgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 49: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: BehaviourWhat we want (our “contract” for the Cat class):

Meow!the bedSgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 50: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: BehaviourWhat we want (our “contract” for the Cat class):

Meow!the bedSgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 51: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: BehaviourWhat we want (our “contract” for the Cat class):

Meow!the bedSgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 52: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...end

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 53: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def speak endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 54: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def speak puts 'Meow!' endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 55: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...end

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 56: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def move_to(location) # ... endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 57: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def move_to(location) @location = location endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 58: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def move_to(location) @location = location end

def location # ... endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 59: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def move_to(location) @location = location end

def location @location endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 60: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...end

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 61: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def eat(food) # ... endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 62: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...

def eat(food) @foods << food endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 63: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def initialize(name, coat, weight) # ... end

def eat(food) @foods << food endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 64: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def initialize(name, coat, weight) # ... @foods = [] end

def eat(food) @foods << food endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 65: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Page 66: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 67: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bedclass Cat

# ...end

Page 68: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ...end

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 69: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def information # ... endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 70: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def information "#{@name} has eaten" endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 71: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def information "#{@name} has eaten" endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 72: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def information "#{@name} has eaten #{@foods.join(', ')}" endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 73: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def information "#{@name} has eaten #{@foods.join(', ')}" endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 74: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

class Cat # ... def information "#{@name} has eaten #{@foods.join(', ')} and is currently here: #{location}" endend

sgt_fuzzyboots = Cat.new('Sgt. Fuzzyboots', 'tabby', 3.1)sgt_fuzzyboots.speaksgt_fuzzyboots.move_to('the bed')puts sgt_fuzzyboots.locationsgt_fuzzyboots.eat('cat food')sgt_fuzzyboots.eat('a treat')puts sgt_fuzzyboots.information

Classes in Ruby: Behaviour

Sgt. Fuzzyboots has eaten cat food, a treat and is currently here: the bed

Page 75: Ruby Monstas · Classes / Objects and OOP Exercises. Recap. Questions Blocks Symbols Ranges. Object-oriented programming Classes & Objects. Cookie cutter analogy. Cookie cutter analogy

Time to practice

Let’s get to it!