DOC PREVIEW
UW CSE 341 - Lecture Notes

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 341:Programming LanguagesDan GrossmanSpring 2008Lecture 20— Introduction to RubyDan Grossman CSE341 Spring 2008, Lecture 20 1'&$%TodayWhy Ruby?Some basics of Ruby programs• Syntax• Classes, Methods• Variables, fields, scope• Dynamic typing• The rep-loop, the main class, etc.Note: Read Thomas book chapters 1–9 (or free first edition 1–8)• Skip/skim regexps and ranges• Not every detail: focus on OO, dynamic typing, blocks, mixinsDan Grossman CSE341 Spring 2008, Lecture 20 2'&$%Ruby• Pure object-oriented: all values are objects• Class-based• Dynamically typed• Convenient reflectionA good starting point for discussing what each of these means andwhat other languages look like.dynamically typed statically typedfunctional Scheme MLobject-oriented Ruby JavaDan Grossman CSE341 Spring 2008, Lecture 20 3'&$%Ruby vs. SmalltalkSmalltalk, unchanged since 1980, is also pure OO, class-based,dynamically-typed.• Smalltalk: tiny language (smaller than Scheme), elegant, regular,can learn whole thing• Smalltalk: integrated into cool, malleable GUI environment• Ruby: large language with a “why not?” attitude• Ruby: scripting language (light syntax, some “odd” scope rules)• Ruby: very popular, massive library support especially for strings,regular expressions, “Ruby on Rails”– Won’t be our focus at all• Ruby: mixins (a cool, advanced OO modularity feature)• Ruby: blocks, libraries encourage lots of FP idiomsDan Grossman CSE341 Spring 2008, Lecture 20 4'&$%Really key ideas• Really, everything is an object (with constructor, fields, methods)• Every object has a class, which determines how the objectresponds to messages.• Dynamic typing (everything is an object)• Dynamic dispatch (focus of next lecture)• Sends to self (a special identifier; Java’s this)• Everything is “dynamic” – evaluation can add/remove classes,add/remove methods, add/remove fields, etc.• Blocks are almost first-class anonymous functions (later)– Can convert to/from real lambdas (class Proc)(Also has some more Java/C like features – loops, return, etc.)Dan Grossman CSE341 Spring 2008, Lecture 20 5'&$%Lack of variable declarationsIf you assign to a variable in scope, it’s mutation.If the variable is not in scope, it gets created (!)• Scope is the method you are inSame with fields: an object has a field if you assign to it• So different objects of the same class can have different fields (!)This “cuts down on typing” but catches fewer bugs (misspellings)• A hallmark of “scripting languages” (an informal term)Dan Grossman CSE341 Spring 2008, Lecture 20 6'&$%Protection?• Fields are inaccessible outside of instance– Define accessor/mutator methods as desired∗ Use attr_read and attr_writer– Good OO design: subclasses can override accessors/mutators• Methods are public, protected, or private– protected: only callable from class or subclass object– private: only callable from self• Later: namespace management, but no hidingDan Grossman CSE341 Spring 2008, Lecture 20 7'&$%Unusual syntaxJust a few random things (keep your own mental list):• Variables and fields are written differently (@ for fields)– @@ for class fields (Java’s static fields)• Newlines often matter — need extra semicolons, colons, etc. toput things on one line• Message sends do not need parentheses (especially with 0arguments)• Operators like + are just message sends• Class names must be capitalized• self is Java’s this• ...Dan Grossman CSE341 Spring 2008, Lecture 20


View Full Document

UW CSE 341 - Lecture Notes

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Racket

Racket

25 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?