Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

Types in Ruby and other languages….  Classes and objects (vs prototypes)  Instance variables/encapsulation  Object creation  Object equality/comparison.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 21 Late Binding; OOP as a Racket Pattern Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Winter 2014.
CSE341: Programming Languages Introduction To Ruby; Dynamic OOP; "Duck Typing" Alan Borning Autumn 2012 (slides borrowed from Dan Grossman)
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Inheritance using Java
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Also “open class” and other languages….  A module is a named group of methods, constants, and class variables  All classes are modules  Modules are.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Winter 2013.
CSE 341 Programming Languages Introduction to Ruby Zach Tatlock Spring 2014.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
+ Ruby and other programming Languages Ronald L. Ramos.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Introduction to Ruby (adapted from CSE 341, Dan Grossman) 1.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Session 6 Comments on Lab 3 & Implications of Inheritance.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
CSE 341 Programming Languages More Ruby Zach Tatlock Spring 2014.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
Ruby Classes, Modules & Mixins
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
Alan Borning Spring 2018 (slides borrowed from Dan Grossman)
CSE341: Programming Languages Ruby: Blocks & Procs; Inheritance & Overriding Alan Borning Spring 2018.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Spring 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Spring 2013.
More About Inheritance & Interfaces
Ruby Classes.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008

Overview Next big topic is typing, classes, and inheritance But first, a couple of useful things  Shorthand for getters/setters  An example of an “each” iterator  A little more about blocks vs. Procs

Getters/Setters Recall that all instance variables are really private – need to define methods to access them class PosRat def initialize(num, = = denom end def end def = value end …

An Alternative Was: def end def end … Instead, can use attr_reader :num, :denom There is a similar attr_writer shortcut

Iterator Example Suppose we want to define a class of Sequence objects that have a from, to, and step, and contain numbers x such that  from <= x <= to, and  x = from + n*step for integer value n (Credit: Ruby Programming Language, Flanagan & Matsumoto)

Sequence Class & Constructor class Sequence # mixin all of the methods in Enumerable include Enumerable def initialize(from, @step = from, to, step end …

Sequence each method To add an iterator to Sequence and make it also work with Enumerable, all we need is this: def each x while x yield x x end

Blocks & Procs Revisited Blocks are only usable in the immediate context of their caller thing.each { | x | do_something_with(x) } Procs are real “first-class” objects  Create with lambda or Proc.new  Proc instances all have a “call” method  Can be stored in fields, passed as arguments, etc.  This is exactly a closure

Types in Ruby Ruby is dynamically typed – everything is an object Only notion of an object’s “type” is what messages it can respond to  i.e., whether it has methods for a particular message  This can change dynamically for either all objects of a class or for individual objects

Duck Typing “If it walks like a duck and talks like a duck, it must be a duck”  Even if it isn’t  All that matters is how an object behaves (i.e, what messages it understands)

Thought Experiment (1) What must be true about x for this method to work? def foo x x.m + x.n end

Thought Experiment (2) What is true about x? x.m + x.n Less than you might think  x must have 0-argument methods m and n  The object returned by x.m must have a + method that takes one argument  The object returned by x.n must have whatever methods are needed by x.m.+ (!)

Duck Typing Tradeoffs Plus  Convenient, promotes code reuse  All that matters is what messages an object can receive Minus  “Obvious” equivalences don’t hold: x+x, 2*x, x*2  May expose more about an object than might be desirable (more coupling in code)

Classes & Inheritance Ruby vs Java:  Subclassing in Ruby is not about type checking (because of dynamic typing)  Subclassing in Ruby is about inheriting methods Can use super to refer to inherited code See examples in points.rb  ThreeDPoint inherites methods x and y  ColorPoint inherits distance methods

Overriding With dynamic typing, inheritance alone is just avoiding cut/paste Overriding is the key difference  When a method in a superclass makes a self call, it resolves to a method defined in the subclass if there is one  Example: distFromOrigin2 in PolarPoint

Ruby Digression Since we can add/change methods on the fly, why use a subclass? Instead of class ColorPoint, why not just add a color field to Point?  Can’t do this in Java  Can do it in Ruby, but it changes all Point instances (including subclasses), even existing ones  Pro: now all Point classes have a color  Con: Maybe that breaks something else