DOC PREVIEW
UW CSE 341 - Lecture 20 Summary

This preview shows page 1 out of 3 pages.

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

Unformatted text preview:

CSE 341, Spring 2008, Lecture 20 SummaryStandard Disclaimer: These comments may prove useful, but certainly are not a complete summary of allthe important stuff we did in class. They may make little sense if you missed class, but hopefully will helpyou organize and process what you have learned.This dislaimer is especially true for this sort of introductory “get used to how Ruby feels”lecture.This lecture is an introduction to Ruby. The corresponding code demonstrates many different languagefeatures, and the course textbook also provides a quite reasonable introduction to the language. Therefore,this summary will focus more on some of the concepts we considered rather than explaining every languagefeature we discussed. Our overlap with the textbook will be roughly chapters 1–9 (or chapters 1–8 of thefirst edition), but we will not cover regexps or ranges. Also, our focus will be on the overall concepts ofobject-oriented programming, dynamic typing, blocks, and mixins, rather than small language details.Ruby is:• Pure object-orinted: all values are objects. In Java, some values that are not objects are null, 13,true, and 4.0. In Ruby, every expression evaluates to an object.• Class-based: Every object is an instance of a class. An object’s class determines what methods anobject has. As in Java, you call a method “on” an object, e.g., obj.m(3,4) evaluates the variable objto an object and calls its m method with arguments 3 and 4.• Pure object-oriented (again): Most expressions are either variables (looking an object in the environ-ment) or method calls. I n fact, 3+4 is just syntactic sugar for 3.+(4), i.e., calling the + method on the3 object with argument 4, which is itself an object.• Dynamically typed: You can call any method on any object. If the object’s class does not define such amethod (or if the number of arguments is wrong), it is a run-time error, just as in Scheme many thingsare run-time errors. As such, we do not have types for method arguments, method results, fields, etc.Everything is an object.• Convenient reflection: Various built-in methods make it easy to discover at run-time properties aboutobjects. As examples, every object has a method class that returns the object’s class, and a methodmethods that returns an array of the object’s methods.• A “scripting language”: This term defies a precise definition. It means the language is engineeredtoward making it easy to write short programs, providing convenient access to manipulating files andstrings. It also does not require that you declare variables before using them (more on this below).Recall that after learning ML, Scheme, Ruby, and Java, you will have seen all four combinations of functionalvs. object-oriented and statically vs. dynamically typed.Our focus will be on Ruby’s object-oriented nature, not on its benefits as a scripting language. Wealso won’t discuss at all its support for building web applications, which is a main reason it is currently sopopular. As an object-oriented language, Ruby shares much with Smalltalk, a language that has basicallynot changed since 1980. Ruby does have some nice additions we will study, such as mixins.Ruby is also a large language with a “why not” attitude, especially with regard to syntax. ML andScheme (and Smalltalk) adhere rather strictly to certain traditional programming-language principles, suchas defining a small language with powerful features that programmers can then use to build large libraries.Ruby often takes the opposite view. For example, there are many different ways to write an if-expression.Here are a few notes on syntax that can seem “unusual” after seeing Java, ML, and Scheme:• You can call a zero-argument method with e.m() or e.m, i.e., the parentheses are optional.• There are several forms of conditional expressions, including e1 if e2 (all on one line), which evaluatese1 only if e2 is true (i.e., it reads right-to-left).1• Newlines are often significant. For example, you can writeif e1e2elsee3endBut if you want to put this all on one line you need to write if e1 then e2 else e3 end, orif e1 : e2 else e3 end. Note, however, indentation is never significant (only a matter of style).• Field names always begin with the @ character (e.g., @foo), class names begin with a capital letter, andmethod and variable names begin with a lower-case letter. Finally, class fields (same idea as Java’sstatic fields) start with @@.• You can define a method with a name that ends in =, for example:def foo= x@blah = x * 2endAs exp ec ted, you can write e.foo=(17) to change e’s foo field to b e 34. Better yet, you can adjust theparentheses and spacing to write e.foo = 17. This is just syntactic sugar. It “feels” like an assignmentstatement, but it is really a method call. Stylistically you do this for methods that mutate an object’sstate in some “simple” way (like setting a field).• Where you write this in Java, you write self in Ruby.• A class’s methods do not all have to be defined in the same place. If you write class Foo ... endmultiple times in a program, all the methods are part of class Foo. (Any repeated methods replaceearlier definitions, even for instances of the class that have already been created.)Some object basics:• To create a new object, call Foo.new with any number of arguments where Foo is a class. This willcreate an object and then call its initialize method. That is, initialize is the one constructor forthe object. The number of arguments it takes is the number that should be pass ed to new (though likeall methods, you can define default arguments, allowing callers to pass fewer arguments).• A class definition contains methods, but does not define what fields are in instances of the class. Anymethod of the class that assigns to a field makes self have that field, even if it didn’t have it before.So different instances of a class can have different fields. Now, typically one does not use this flexibility.If initialize always assigns to some field, then all instances will have that field. And if all methodsassign only to fields always assigned to by initialize, then all instances of a class will have the samefields.• Methods can be public, protected, or private. Any code can invoke an object’s public methods.Only other methods defined in the same class or a subclass (we’ll see subclassing next lecture) caninvoke a protected method (else it’s a run-time error).


View Full Document

UW CSE 341 - Lecture 20 Summary

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 20 Summary
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 20 Summary 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 20 Summary 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?