Ruby Classes, Modules & Mixins

Slides:



Advertisements
Similar presentations
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Advertisements

CSE 303 Lecture 16 Multi-file (larger) programs
Writing and Testing Programs Drivers and Stubs Supplement to text.
Scripting Languages For Virtual Worlds. Outline Necessary Features Classes, Prototypes, and Mixins Static vs. Dynamic Typing Concurrency Versioning Distribution.
Abstract Data Types and Encapsulation Concepts
Distribution of Marks Internal Sessional Evaluation Assignments – 10 Quizzes – 10 Class Participation Attendence – 5 Mid – Term Test – 25 External Evaluation.
Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class.
Object Oriented Programming
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
CS2110: SW Development Methods Design of methods (functions) Class design – CRC cards – UML class and sequence diagrams Software Design.
Introduction to Object-oriented programming and software development Lecture 1.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 11 : Frameworks SWE 316: Software Design and Architecture  To understand.
Ruby! Useful as a scripting language – script: A small program meant for one time use – Targeted towards small to medium size projects Use by: – Amazon,
Program Design CMSC 201. Motivation We’ve talked a lot about certain ‘good habits’ we’d like you guys to get in while writing code. There are two main.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
Also “open class” and other languages….  A module is a named group of methods, constants, and class variables  All classes are modules  Modules are.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Java Fundamentals Usman Ependi UBD
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
ENEE150 – 0102 ANDREW GOFFIN Functional Decomposition.
Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.
Introduction to OOP CPS235: Introduction.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Virtual Memory Pranav Shah CS147 - Sin Min Lee. Concept of Virtual Memory Purpose of Virtual Memory - to use hard disk as an extension of RAM. Personal.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
Object- oriented Design Principles
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Patterns Protect from Change Rule: if something is going to change, make it an object. Strategy: make algorithm an object so it can change State: make.
©MAK Technologies, Inc. The Dynamic-Link-Compatible C++ RTI API for IEEE 1516 Len Granowetter MÄK Technologies x121.
Lecture 3 – MapReduce: Implementation CSE 490h – Introduction to Distributed Computing, Spring 2009 Except as otherwise noted, the content of this presentation.
Also “open class” and other languages…
CSE 219 Final exam review.
CSC 222: Object-Oriented Programming
Visit for more Learning Resources
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Classes and OOP.
Behavioral Design Patterns
CSE 341 Section 9 Nick Mooney Spring 2017
Topic: Functions – Part 2
CS1010 Programming Methodology
CS1010 Programming Methodology
Interfaces and Inheritance
Object Oriented Practices
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Present by Andie Saizan, MCP
Chapter 6 Methods: A Deeper Look
CSE 341 Section 9 Winter 2018 Adapted from slides by Eric Mullen, Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
Object Oriented Practices
Unit 3: Lesson 6 & 7- Functions and Top-Down Design / APIs and Function Parameters Day 27.
Namespaces – Local and Global
Nicholas Shahan Spring 2016
Topics Introduction to Functions Defining and Calling a Function
Records and Type Classes
CSE 341 Section 9 Fall 2017 Adapted from slides by Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
Namespaces – Local and Global
Records and Type Classes
Presentation transcript:

Ruby Classes, Modules & Mixins CSE 413 Autumn 2008 Ruby Classes, Modules & Mixins

Organizing Large(r) Programs Issues Need to divide code into manageable pieces Want to take advantage of reusable chunks of code (libraries, classes, etc.) Strategy: Split code into separate files Typically, one or more classes per file But what if the parts don’t really form a class?

Namespaces & Modules Idea: Want to break larger programs into pieces where names can be reused independently Avoids clashes combining libraries written by different organizations or at different times Ruby solution: modules Separate source files that define name spaces, but not necessarily classes

Example (from Programming Ruby) module Trig PI = 3.14 def Trig.sin(x) # … end def Trig.cos(x) module Moral VERY_BAD = 0 BAD = 1 def Moral.sin(badness) # … end

Using Modules Key point: Each module defines a namespace # … require ‘trig’ require ‘moral’ y = Trig.sin(Trig::PI/4) penance = Moral.sin( Moral::VERY_BAD) Key point: Each module defines a namespace No clashes with same names in other modules Module methods are a lot like class methods

Mixins Modules can be used to add behavior to classes – mixins Define instance methods and data in module “include” the module in a class – incorporates the module definitions into the class Now the class has its original behavior plus whatever was added in the mixin Provides most of the capabilities of multiple inheritance and/or Java interfaces

Example module Debug def trace # … end class Something include debug class SomethingElse include debug # … end Both classes have the trace method defined, and it can interact with other methods and data in the class

Exploiting Mixins – Comparable The real power of this is when mixins build on or interact with code in the classes that use them Example: library mixin: Comparable Class must define operator <=> (a <=> b returns -1, 0, +1 if a<b, a==b, a>b) Comparable uses <=> to define <, <=, ==, >=, >, and between? for that class

Another example – Enumerable Container/collection class provides an each method to call a block for each item in the collection Enumerable module builds many mapping-like operations on top of this map, include?, find_all, … If items in the collection implement <=> you also get sort, min, max, …