CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Introduction to Ruby (adapted from CSE 341, Dan Grossman) 1.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

John Pinney Ruby in 20 minutes John Pinney
1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
The Ruby Programming Language
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CMSC 330: Organization of Programming Languages Ruby Hash Tables (and other topics)
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 4 – Shell Variables, More Shell Scripts.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
01/24/2005 Introduction to Programming with Java, for Beginners A Closer Look at Constructors and Methods.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Programming Languages Dan Grossman 2013 Everything is an Object.
Programming Languages Dan Grossman 2013 Classes and Objects.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE 374 Programming Concepts & Tools
Programming Languages Dan Grossman 2013
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
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 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Java Primer 1: Types, Classes and Operators
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 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 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
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 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
Adapted from slides by Nicholas Shahan and Dan Grossman
Tonga Institute of Higher Education
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
Java Programming Language
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
CSE 3302 Programming Languages
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Introduction to Ruby (adapted from CSE 341, Dan Grossman) 1

The Plan Why Ruby? Some basics of Ruby programs –Syntax –Classes, methods –Fields, variables, scope –Dynamic typing We won’t cover all (or most) of the details in class Focus on OO, dynamic typing, blocks, mixins References: online library docs + –Thomas Programming Ruby (1 st ed online, ch 1-8, 2 nd ed, v1.8, chs. 1-9, 3 rd ed, v1.9, chs. 1-10) Electronic copies available from publisher’s site 2

Logistics We’ll use Ruby –Ruby 1.9 has some differences we’ll mention, but core ideas are the same REPL (irb) + full Ruby Installation instructions, etc. on course web –Windows: use “one click installer” –OS X: part of developer tools if you don’t have it –Linux: use your favorite package manager 3

Why? Because: –Pure object-oriented language Interesting, not entirely obvious implications –Interesting design decisions Type system, mixins, syntax (“friendly”), etc. Also interesting, but we’re skipping: RAILS web framework –Major reason for industry interest in Ruby, but no time to cover (would take a month) –But you should be able to pick it up after 413

Where Ruby fits Design choices for O-O and functional languages Dynamic typed OO helps isolate OO’s essence without details of type system Historical note: Smalltalk –Classic dynamically typed, class-based, pure OO –Ruby takes much from this tradition dynamically typedstatically typed functionalScheme/RacketHaskell, ML (not in 413) object-orientedRubyJava

Ruby key ideas (1) Everything is an object (with constructor, fields, methods), even numbers, even classes(!) Contrast w/Java primitive vs reference types Class based: every object has a class, which determines how it responds to messages –Like Java, not like Javascript Dynamic typing –vs static typing in Java Convenient reflection (runtime inspection of objects) Dynamic dispatch (like Java) Sends to self (same as this in Java)

Ruby Key Ideas (2) Everything is “dynamic” –Evaluation can add/remove classes, add/remove methods, add/remove fields, etc. Blocks and libraries encourage use of closure idioms Blocks are almost first-class anonymous function closures –Can convert to/from real lambdas mixins: neat, advanced modularity feature Syntax and scoping rules of a “scripting language” –Often many ways to say something – “why not” attitude –Variables “spring to life” on first use –Some interesting (odd?) scoping rules And a few C/Java-like features (loops, return, etc.)

Defining a class (see web for full example) class Rat = # no instance variable (field) declarations # just assign to create field foo def initialize (num, den=1) = = den end def print … end def add r … edn end 8

Using a class (1) ClassName.new(args) creates a new instance of ClassName and calls its initialize method with args Every variable references an object (possibly the nil object) –Local variables (in a method) foo –Instance variables –Class variables (static fields) –Global variables and constants $foo $MAX 9

Using a class (2) You use an object with a method call –Also known as message send –Object’s class determines its behavior Examples: x.m 4 x.m1.m2(y.m3) -42.abs –m and m(…) are syntactic sugar for self.m and self.m(…) –e1+e2 is sugar for e1.+(e2) (yup, really!!!) 10

No Variable Declarations If you assign to a variable, it’s mutation If the variable is not in scope, it is created(!) (Do not mispeal things!!) –Scope of new variable is the method you are in Same with fields: if you assign to a field, that object has that field –So different objects of the same class can have different fields(!) Fewer keystrokes in programs, “cuts down on typing”, but compiler catches fewer bugs –A hallmark of “scripting languages”

Protection? Fields are inaccessible outside (individual) instances –All instance variables are private –Define getter/setter methods as needed Methods are public, protected, private –public is the default –protected: only callable from class or subclass object –private: only callable from self –Protected & private differ from Java (how?)

Getters and setters If you want outside access, must define methods def foodef = xend The foo= convention allows sugar via extra spaces x.foox.foo = 42 Shorter syntax for defining getters/setters attr_reader :foo attr_writer :foo Overall, requiring getters/setters is more uniform, OO –Can change methods later without changing clients 13

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 an existing class that is then visible to all instances of that class Changing a class affects all instances – even if already created –Disastrous example: changing Fixnum ’s + method Overall: a simple language where everything can be changed and method lookup uses instance’s classes 14

Unusual syntax (add to this list as you discover things) Newlines often matter – example: don’t need semi- colon if a statement ends a line Message sends (function calls) with 0 or 1 arguments often don’t need parentheses Infix operations like + are just message sends Can define operators including = [ ] Conditional expressions e1 if e2 and similar things (as well is if e1 then e2)

Unusual syntax (add to this list as you discover things) Classes don’t need to be defined in one place (similar to C#, not Java, C++) Class names must be capitalized self is Java’s “this” Loops, conditionals, classes, methods are self- bracketing (end with end ) –Actually not unusual except for programmers with too much exposure to C/Java/C#/C++ and other languages of the curly brace persuasion

A bit about Expressions Everything is an expression and produces a value nil means “nothing”, but it is an object (an instance of class NilClass ) nil and false are false in a boolean context; everything else is true (including 0) ‘strings’ are taken literally (almost) “strings” allow more substitutions –including #{expressions} –(Elaborate regular expression package. Won’t cover in class but learn/use if you wish.)

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 can “get started” rather than requiring creating an object and calling a method from within irb Top-level methods are added to Object, which makes them available everywhere irb: Ruby REPL/interpreter –Use load “filename.rb” to read code from file 18