1 of 14

Modules and Inheritance

2 of 14

Being Lazy

is what it’s all about

1

3 of 14

Inheritance

  • Share functionality between classes
  • <subclass> is a <superclass>. <child> is a <parent>
  • Everything inherits from Object and BasicObject
  • Functionality for free!

4 of 14

Everything is an Object

5 of 14

Inheritance Code Examples

class Person

attr_reader :first_name, :last_name

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end

def full_name

"#{first_name} #{last_name}"

end

end

class Programmer < Person

end

inheriting functionality

from Person

6 of 14

Inheritance Code Examples

class Person

attr_reader :first_name, :last_name

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end

def full_name

"#{first_name} #{last_name}"

end

end

class Programmer < Person

def full_name

"#{first_name} #{last_name}, coder of things!"

end

end

overriding a method

from Person

7 of 14

Inheritance Code Examples

class Person

attr_reader :first_name, :last_name

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end

def full_name

"#{first_name} #{last_name}"

end

end

class Programmer < Person

def full_name

"#{super}, coder of things!"

end

end

using parent method output

in our new method

8 of 14

Inheritance Code Examples

class Person

attr_reader :first_name, :last_name

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end

def greet(someone)

"Hi there #{someone}"

end

end

class Programmer < Person

def greet(someone)

"#{super(someone)}! Want to pair program?!"

end

end

using super with arguments

9 of 14

Inheritance Code Examples

class Person

attr_reader :first_name, :last_name

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end

end

class Programmer < Person

def initialize(f_name, l_name, fav_language)

super(f_name, l_name)

@fav_language = fav_language

end

end

overriding initialize method

10 of 14

Inheritance Code Examples

class Person

attr_reader :first_name, :last_name

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end

def full_name

"#{first_name} #{last_name}"

end

end

class Programmer < Person

def full_name

"#{super}, coder of things!"

end

def to_s

full_name

end

end

overriding a method

from Object

11 of 14

Practical Examples

  • Everything inherits Object and BasicObject
  • Fixnum inherits from Numeric
  • Float inherits from Numeric

12 of 14

Modules

  • Share functionality between classes
    • Focused on sharing specific methods
    • Used for situations where you can’t apply the “is a” connection between the two classes
  • include instance methods
  • extend class methods

13 of 14

Practical Examples

  • Array mixes in Enumerable
  • Hash mixes in Enumerable
  • each_with_index for Arrays
  • including functionality provided by external libraries (i.e. provided through gems)
    • i.e. include HTTParty - now you have Ruby methods for making HTTP requests!

14 of 14

When to use modules and inheritance?

  • Largely used to integrate well-documented,�external libraries
  • Don’t “over-optimize”!
    • Beware of too many dependencies!
    • Is this module likely to change much?
    • Does this module have good methods and interfaces?
  • Start by writing your code w/o modules, �and then afterwards refactor if needed