CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.

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

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C 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 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Object-Oriented PHP (1)
CSE341: Programming Languages Introduction To Ruby; Dynamic OOP; "Duck Typing" Alan Borning Autumn 2012 (slides borrowed from Dan Grossman)
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
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.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
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.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
CSE 341 Programming Languages More Ruby Zach Tatlock Spring 2014.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
ISBN Chapter 12 Support for Object-Oriented Programming.
Programming Languages Dan Grossman 2013 Everything is an Object.
Programming Languages Dan Grossman 2013 Classes and Objects.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP
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.
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 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 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 Spring 2016.
More Object-Oriented Programming
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 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping 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
Presentation transcript:

CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011

The plan Will still use Racket for some more topics, but first get up-to- speed on Ruby –Do now to better align with homework and section schedule Lecture materials may not recount every little language feature we use –Thomas book (2 nd edition, Chapters 1-9) quite readable Can skip/skim regexps and ranges Also see online library documentation [large, searchable] Focus in class will be on OOP, dynamic typing, blocks, mixins Fall 20112CSE341: Programming Languages

Logistics We will use Ruby –Ruby 1.9 is not compatible, but not hugely different –"The real world" is still using both a lot –Homework 6's graphics (mandatory) won't work with 1.9 Installation instructions, etc. on course web-page –Can run programs with a REPL called irb Homework 6 is about understanding and extending an existing program in an unfamiliar language –Good practice; different than previous homeworks –Read code: determine what you do and don't (!) need to know Fall 20113CSE341: Programming Languages

Ruby Pure object-oriented: all values are objects (even numbers) Class-based: Every object has a class that determines behavior –Like Java, unlike Javascript –Mixins (neither Java interfaces nor C++ multiple inheritance) Dynamically typed Convenient reflection: Run-time inspection of objects Blocks and libraries encourage lots of closure idioms Syntax and scoping rules of a "scripting language" –Often many ways to say the same thing –Variables "spring to life" on use –Lots of support for string manipulation [we won't do this] Popular for building server-side web applications –But we won't discuss Ruby on Rails Fall 20114CSE341: Programming Languages

Where Ruby fits Note: Racket also has classes and objects when you want them –In Ruby everything uses them (at least implicitly) Historical note: Smalltalk also a dynamically typed, class-based, pure OOP language with blocks and convenient reflection –Smaller just-as-powerful language –Contrast Ruby's "why not add that" attitude Probably less elegant; perhaps more useful Dynamically typed OO helps identify OO's essence by not having to discuss types Fall 20115CSE341: Programming Languages dynamically typedstatically typed functional Racket SML object-oriented Ruby Java

Defining a class [For full code details and various expression constructs, see lec19.rb] Fall 20116CSE341: Programming Languages class Rational = # no instance variable (field) decls # just assign to create field foo def initialize (num,den=1) = = den end def print … end def add r … end … end

Using a class ClassName.new(args) creates a new instance of ClassName and calls its initialize method with args Every variable holds an object (possibly the nil object) –Local variables (in a method) foo –Instance variables –Class variables (static fields) You use an object with a method call –Also known as a message send –Every object has a class, which determines its behavior Examples: x.m 4 x.m1.m2(y.m3) -42.abs –m and m(…) are sugar for self.m and self.m(…) –e1 + e2 is sugar for e1.+(e2) (really!) Fall 20117CSE341: Programming Languages

Method / variable visibility private : only available to object itself protected : available only to code in the class or subclasses public : available to all code This is different than what the words mean in Java All instance variables and class variables are private Methods are public by default –There are multiple ways to change a method's visibility Fall 20118CSE341: Programming Languages

Some syntax / scoping gotchas You create variables (including instance variables) implicitly by assigning to them –So a mis-spelling just creates a new variable –Different instances of a class could have different fields Newlines matter –Often need more syntax to put something on one line –Indentation is only style (not true in some languages) Class names must be capitalized Message sends with 0 or 1 argument don't need parentheses self is a special keyword (Java's this) Fall 20119CSE341: Programming Languages

Getters and setters If you want outside access to get/set instance variables, must define methods The foo= convention allows sugar via extra spaces when using the method Shorter syntax for defining getters and setters is: Overall, requiring getters and setters is more uniform and more OO –Can change the methods later without changing clients –Particular form of change is subclass overriding [next lecture] Fall CSE341: Programming Languages def end def foo= = a end x.foo = 42 x.foo attr_writer :foo attr_reader :foo

Top-level Expressions at top-level are evaluated in the context of an implicit "main" object with class Object That is how a standalone program would "get started" rather than requiring an object creation and method call from within irb Top-level methods are added to Object, which makes them available everywhere Fall CSE341: Programming Languages

Class definitions are dynamic All definitions in Ruby are dynamic Example: Any code can add or remove methods on existing classes –Very occasionally useful (or cute) to add your own method to the Array class for example, but it is visible to all arrays Changing a class affects even already-created instances Disastrous example: Changing Fixnum 's + method Overall: A simple language definition where everything can be changed and method lookup uses instance's classes Fall CSE341: Programming Languages

Duck Typing "If it walks like a duck and quacks like a duck, it's a duck" –Or don't worry that it may not be a duck When writing a method you might think, "I need a Foo argument" but really you need an object with enough methods similar to Foo 's methods that your method works –Embracing duck typing is always making method calls rather than assuming/testing the class of arguments Plus: More code reuse; very OO approach –What messages an object receive is all that matters Minus: Almost nothing is equivalent –x+x versus x*2 versus 2*x –Callers may assume a lot about how callees are implemented Fall CSE341: Programming Languages

Duck Typing Example Fall CSE341: Programming Languages def mirror_update pt pt.x = pt.x * (-1) end Natural thought: "Takes a Point object (definition not shown here), negates the x value" –Makes sense, though a Point instance method more OO Closer: "Takes anything with getter and setter methods instance variable and multiplies the x field by -1 " Closer: "Takes anything with methods x= and x and calls x= with the result of multiplying result of x and -1 Duck typing: "Takes anything with method x= and x where result of x has a * method that can take -1. Sends result of calling x the * message with -1 and sends that result to x= "