Presentation is loading. Please wait.

Presentation is loading. Please wait.

Also “open class” and other languages…

Similar presentations


Presentation on theme: "Also “open class” and other languages…"— Presentation transcript:

1 Also “open class” and other languages…
Ruby Modules Also “open class” and other languages…

2 Open class In another file:
Methods and variables can be added to classes In RubyOpenClasses-1.rb: class Cat attr_accessor :name, :age def initialize(name, age) = name, age end In another file: require_relative "RubyOpenClasses-1.rb" class Cat def purr puts "#{name} says Purrrrrrrrrrr" end ollie = Cat.new("Ollie", 5) ollie.purr

3 Liskov Substitution Principle
Helps to guide best practices for Ruby open classes LSP (as we know it): subtypes must be substitutable for base class Goal: don’t break program when call a method that expects a parent object but receives a child object Remember: square vs rectangle example. LSP: instances of a class after open/extend must behave like instances of a class before From:

4 LSP Example LSP Violation: puts (1/2) => 0
NOW open file that requires "mathn" puts (1/2) => (1/2) On your paper: What class was modified in this example?

5 LSP example 2 In another file: In RubyOpenClasses-1.rb:
class Cat #attr_accessor :name, :age def initialize(name, age) = name, age end def old @age > 10 jane = Cat.new("Jane", 12) if jane.old puts "sleeping" else puts "playing" In another file: require_relative "RubyOpenClasses-1.rb" class Cat def old puts "cats never grow old" end jane = Cat.new("Jane", 12) if jane.old puts "sleeping" else puts "playing" New example, not in your ruby files

6 Modules A module is a named group of methods, constants, and class variables All classes are modules Modules are not classes Can’t be instantiated Can’t be subclassed Purpose: namespaces and mixins

7 Metamodel Object Module Class Numeric Integer Fixnum 4
From: Seven Languages in Seven Weeks

8 Purpose #1: Namespace Skim: On your paper:
On your paper: How do we specify a namespace in Java? How do we specify a namespace in C++? Purpose of namespace is exam material (not so concerned about syntax)

9 Namespace in Ruby Methods in a class already have implicit namespace (the class). What if we want to provide utility functions that don’t require an object. Maybe encode/decode functions, conversions, math functions, etc. In Java we’d have to create a class and add static method (e.g., Math.abs). In Ruby we could add global methods, but then we’d need to worry about naming conflicts. Ruby solution: module (provides namespace)

10 Example from text module Base64 # end of module Call as:
text = Base64.encode(data) data = Base64.decode(text) module Base64 def self.encode (data) # code here end #could be: Base64.decode # cannot just leave off self def self.decode (text) # end of module RubyModules.rb

11 Purpose #2: Mixins Skim: On your paper:
Be sure to follow link to: #The_diamond_problem On your paper: What’s one purpose/goal of a mixin? What is the diamond problem and how do mixins avoid? Think (not on website): how is this similar/different from Java interfaces? Exam material

12 Multiple Inheritance Languages like C++ allow a child to have two parents The purpose is to support code reuse when a class has a logical “is-a” relationship with multiple classes An issue can arise when a child has two parents with the same method – which one should be called? There are similar issues related to instance variables.

13 Multiple Inheritance, cont.
Due to issues with multiple inheritance (typically called the “diamond problem”), many languages limit classes to having a single parent In place of multiple “is-a” relationships, these languages use features that enable a class to incorporate methods from other entities, but without the entire class hierarchy Java example: interfaces Ruby example: modules used as mixins

14 Mixins in Ruby If a module defines instance methods, those can be “mixed into” other classes Essentially eliminates need for multiple inheritance Examples: Enumerable. defines iterators that make use of each method Comparable. defines comparison operators (<, <=,==, >, >=, between?) in terms of <=> (like compareTo) Module.include function does the “mixing”

15 require vs include TRACE
require brings in the contents of another file (like “include” in C++) include “mixes in” a module module FlyingCreature def fly puts "Flying! with speed end class Bird include FlyingCreature def initialize @speed = 5 #... class Mammal # ... class Bat < Mammal daffy = Bird.new daffy.fly dracula = Bat.new dracula.fly puts "dracula is a flying creature: #{dracula.is_a? FlyingCreature}" piglet = Mammal.new puts "piglet is a flying creature: #{piglet.is_a? FlyingCreature}" TRACE RubyModules-2.rb demo code, example from:

16 Mixin Best Practices Skim: On your paper:
y-class-module-mixins/ On your paper: When should you NOT use a class? Do we create instances of modules? How do we use module instance methods?

17 Modules vs Interfaces On your own:
Interesting discussion, not exam material

18 Singleton Method Example
class Person def end shugo = Person.new("Shugo") matz = Person.new("Matz") # Singleton method, applies to only one object def matz.design_ruby puts "I am and I designed Ruby!" matz.design_ruby shugo.design_ruby # error! From: See also:


Download ppt "Also “open class” and other languages…"

Similar presentations


Ads by Google