Lecture 2. Object Orientation 1 / 51

Similar documents
Lecture 2. Object Orientation 1 / 50

Lecture 2. Object Orientation

Lecture 1. Basic Ruby 1 / 61

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE : Python Programming

Basic Object-Oriented Concepts. 5-Oct-17

CSc 372 Comparative Programming Languages

MITOCW watch?v=flgjisf3l78

Ruby: Object-Oriented Concepts

SYMBOLS. you would not assign a value to a symbol: :name = "Fred" The Book of Ruby 2011 by Huw Collingbourne

Lecture 3. Miscellaneous Ruby and Testing

Classes and Modules. Properties. Chapter 11

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Lecture 4. Ruby on Rails 1 / 52

Lecture 3. Miscellaneous Ruby and Testing 1 / 40

Programming Paradigms

Overview of the Ruby Language. By Ron Haley

Java Object Oriented Design. CSC207 Fall 2014

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

In fact, as your program grows, you might imagine it organized by class and superclass, creating a kind of giant tree structure. At the base is the

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

CSc 372 Comparative Programming Languages

Inheritance. CSc 372. Comparative Programming Languages. 28 : Ruby Classes. Department of Computer Science University of Arizona.

Supporting Class / C++ Lecture Notes

Lecture 3. Miscellaneous Ruby and Testing 1 / 48

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

And Even More and More C++ Fundamentals of Computer Science

Chapter 4 Defining Classes I

S206E Lecture 19, 5/24/2016, Python an overview

Import Statements, Instance Members, and the Default Constructor

Array variable in class ruby. Array variable in class ruby.zip

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CS 231 Data Structures and Algorithms, Fall 2016

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS Exam 1 Review Suggestions

Java: introduction to object-oriented features

Lecture 10. Overriding & Casting About

Super-Classes and sub-classes

EE 422C HW 6 Multithreaded Programming

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Ruby: Introduction, Basics

CSCI 161 Introduction to Computer Science

Lecture 02, Fall 2018 Friday September 7

Announcements for the Class

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Chapter 6 Classes and Objects

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lab 1: Introduction to Java

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

Intro. Classes & Inheritance

COMP 105 Homework: Type Systems

Inheritance and Interfaces

The Stack, Free Store, and Global Namespace

Midterms Save the Dates!

CSC207 Week 3. Larry Zhang

Building custom components IAT351

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Introduction to Programming Using Java (98-388)

contain a geometry package, and so on). All Java classes should belong to a package, and you specify that package by typing:

CPS 506 Comparative Programming Languages. Programming Language

CSE 341: Programming Languages. Section AC with Nate Yazdani

CSE 303: Concepts and Tools for Software Development

Slide 1 CS 170 Java Programming 1 Testing Karel

Chapter 11 Generics. Generics. Hello!

Readings for This Lecture

Object-Oriented Programming. Lecture 17

Summary. Recursion. Overall Assignment Description. Part 1: Recursively Searching Files and Directories

QUIZ. What is wrong with this code that uses default arguments?

6.001 Notes: Section 8.1

CS 5142 Scripting Languages

CS-202 Introduction to Object Oriented Programming

6.001 Notes: Section 15.1

Midterms Save the Dates!

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

Your First Ruby Script

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

CS1004: Intro to CS in Java, Spring 2005

CMSC201 Computer Science I for Majors

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Lecture 10 Declarations and Scope

Lecture 14: more class, C++ streams

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Basics Inheritance and Instance Variables Inheritance and Methods Class Variables. Ruby Inheritance CSCI September 2017.

Transcription:

Lecture 2 Object Orientation 1 / 51

Homework 1 Homework 1 was due at noon You will be graded on: Correctness: 15 points (passing all RSpec tests) Style: 5 points (having no Rubocop style offenses) Best Practices: 5 points (manually graded by TA) For this assignment, you will receive feedback but not actually lose any points Feedback by next Tuesday 2 / 51

How was HW 1? Vote at PollEv.com/CIS196776 3 / 51

O ce Hours https://www.seas.upenn.edu/~cis196/contact/ Likely final but changes will be announced if any come up 4 / 51

CIS19x lecture is after class 6-7:30pm in Towne 100 This week the focus will be Git/Github Next week is the last lecture and will focus on HTML/CSS basics Please att if you are uncomfortable with these subjects 5 / 51

Pry Gem The Pry Gem is a great way to debug your code After installing the gem, you add require 'pry' to the top of a file You can then add binding.pry anywhere in the file, acts like a breakpoint Whenever it reaches that point during execution, it will stop and give you a snapshot of the current state of your code You can execute methods, view currently defined variables, etc. Type exit to continue code execution 6 / 51

Making Pry Better with Pry-Nav Pry-Nav adds next, step, and continue commands to pry and allows you to step through your code No additional setup needed outside of installing the two gems pry-remote and pry-nav 7 / 51

Object Orientation 8 / 51

Classes & Objects Classes define clusters of behavior or functionality Almost everything in Ruby is an object Even types that are primitive in other languages, e.g. ints Because of this, there is always an object from which to call methods Even arithmetic operations are methods Ruby adds syntactic sugar to an expression like 1.+(2) to allow us to call it like 1 + 2 Convention is to use a method whenever you can 9 / 51

Creating a Class Use the class and keywords and place the class's code between them It is convention to use PascalCase when naming classes Create an instance of a class (an object) by calling the new method class Person person = Person.new p person.class #=> Person 10 / 51

Constructors If you define a method named initialize, it will be automatically executed as the constructor. class Person def initialize p 'Hi, I am being initialized!' Person.new #=> "Hi, I am being initialized!" 11 / 51

Constructors with Arguments Arguments passed into the new method are also passed to the constructor class Person def initialize(name) p "You've initialized a person with name: #{name}" Person.new('Sanj') #=> "You've initialized a person with name: Sanj" 12 / 51

Instance Variables Instance variables allow you to remember state in individual objects Their names always start with a single @ They are only visible to the instance to which they belong & every instance has its own set of instance variables No need to declare right at the top before the constructor, just define on the fly class Person def initialize(name) @name = name p "Assigned instance variable: #{@name}" Person.new('Sanj') #=> "Assigned instance variable: Sanj" 13 / 51

Instance Methods Methods that are inted to be called on a specific instance of the class Methods defined in a class are instance methods by default class Person def say_hello p 'Hello' person = Person.new person.say_hello #=> "Hello" 14 / 51

Instance Methods & Instance Variables Instance variables don't have to just be defined in the constructor You can also define instance variables in instance methods Instance variables can be accessed in any instance method regardless of where they've been defined 15 / 51

Instance Methods & Instance Variables class Person def set_name(name) @name = name def get_name @name person = Person.new person.set_name('sanj') p person.get_name #=> "Sanj" Note that the above is actually poor style in Ruby 16 / 51

Reader Methods What we call getter methods in other languages are referred to as reader methods in Ruby Used to return the value of a variable in a class Share name with variable being returned 17 / 51

Reader Methods Reader method has same name as the variable class Person def initialize(name) @name = name def name @name person = Person.new('Sanj') p person.name #=> "Sanj" 18 / 51

Writer Methods What we call setter methods in other languages are referred to as writer methods in Ruby Used to set the value of a variable in the class Naming convention is to share name with variable being set followed by = Ruby uses syntactic sugar to allow us to call the method like it's an assignment 19 / 51

Writer Methods class Person def initialize(name) @name = name def name @name def name=(name) @name = name person = Person.new('Sanj') person.name = 'Bob' p person.name #=> "Bob" 20 / 51

Writer Methods Note that writer methods can do more than just simple assignment class Person def name @name def name=(name) @name = name.length > 2? 'Too long' : name person = Person.new person.name = 'Sanjana' p person.name #=> "Too long" 21 / 51

Attr Readers The use of reader methods in Ruby is pretty common We can use the attr_reader method to automatically generate a reader method i.e. you don't have to define your own reader methods, they're generated on the fly class Person attr_reader :name def initialize(name) @name = name person = Person.new('Sanj') p person.name #=> "Sanj" 22 / 51

Attr Writers We can do the same with writers Using attr_writer will generate a writer method class Person attr_reader :name attr_writer :name person = Person.new person.name = 'Sanj' p person.name #=> "Sanj" 23 / 51

Attr Accessors We can generate both a reader and writer method in one line by using an attr_accessor class Person attr_accessor :name person = Person.new person.name = 'Sanj' p person.name #=> "Sanj" 24 / 51

Which will de ne a reader and writer? # A class Person def name @name def name=(name) @name = name # B class Person attr_reader :name attr_writer :name # C class Person attr_accessor :name # D: All of the above Vote at PollEv.com/CIS196776 25 / 51

Generating Multiple Methods Multiple symbols can be passed to all three of the attr methods This will generate a reader/writer (or both) for all of the symbols that you provide 26 / 51

Generating Multiple Methods class Person attr_reader :name, :age, :favorite_language def initialize(name, age, favorite_language) @name = name @age = age @favorite_language = favorite_language person = Person.new('Sanj', 21, 'Ruby') p person.name #=> "Sanj" p person.age #=> 21 p person.favorite_language #=> "Ruby" 27 / 51

Class Methods A method inted to be called on the class itself (not an instance) is called a class method Defined like an instance method, but starts with self. It cannot call any instance methods class Person def self.average_life_expectancy '71.5 years' p Person.average_life_expectancy #=> "71.5 years" 28 / 51

self Similar to the this keyword in Java There's always exactly one current object or self The current object is determined by the scope 29 / 51

Describing Methods Instance methods are conveyed with #: ClassName#instance_method (i.e. String#capitalize) Class methods are conveyed with ::: ClassName::class_method (i.e. Math::tan) Note that this has nothing to do with the way you define methods You will see this notation in Ruby Docs 30 / 51

Private Methods By default, all methods are public Of course you don't want to be able to access every method outside of the class Methods can be made private with the private keyword In the body of a class, all methods physically under private are private 31 / 51

Private Methods class Person def say_hello "Hi, I'm #{name}" private def name 'Sanj' person = Person.new p person.say_hello #=> "Hi, I'm Sanj" p person.name #=> NoMethodError: private method `name' called for #<Person 32 / 51

Class Variables Denoted with @@ at the beginning Similar to static variables in Java They are preserved across all instances of the class All instances can access and modify them 33 / 51

Class Variables class Person @@count = 0 def initialize @@count += 1 def self.count @@count p Person.count #=> 0 Person.new p Person.count #=> 1 They may seem harmless at first, but they're actually pretty bad practice 34 / 51

Inheritance Classes can inherit from only one other class Classes can be thought of as hierarchical Inheritance is done with the < operator A class will gain all of its parent class's methods, both public and private class Person class Programmer < Person p Programmer.superclass #=> Person 35 / 51

The Problem with Class Variables It turns out that class variables are more accurately described as "class hierarchy variables" Not only does the class have access to the variables, its descants will too This is pretty bad As a general rule, don't use class variables unless you mean for this to happen 36 / 51

The Problem with Class Variables class Bird @@location = 'EVERYWHERE' def self.location @@location p Bird.location #=> "EVERYWHERE" class Penguin < Bird @@location = 'Antarctica' def self.location @@location p Penguin.location #=> "Anarctica" p Bird.location #=> "Antarctica" 37 / 51

How do we work around this? Like I said earlier, almost everything in Ruby is an object All classes are instances of the Class class This means that classes are themselves objects class Person p Person.class #=> Class 38 / 51

Use Class Instance Variables We've already seen that objects can have instance variables Since classes are themselves objects, they too can have their own instance variables We will refer to a class's instance variables as class instance variables Define class instance variables within the class definition body OR within class methods with a single @ Remember that regular instance variables must be defined in an instance method or the constructor 39 / 51

Use Class Instance Variables Always use class instance variables, NEVER use class variables Work essentially like static variable in Java class Bird @location = 'EVERYWHERE' def self.location @location class Penguin < Bird @location = 'Antarctica' p Bird.location #=> "EVERYWHERE" p Penguin.location #=> "Antarctica" 40 / 51

Which is better style? # A class Person @@count = 0 # B class Person @count = 0 Vote at PollEv.com/CIS196776 41 / 51

Modules I mentioned earlier that classes can inherit from only one class This seems really limiting at first, but that's where modules come in Modules are bundles of methods and methods only Declared with the module keyword Unlike classes, modules cannot be instantiated 42 / 51

Modules module MyModule def my_module_method 'Hello World' MyModule.new #=> NoMethodError: undefined method `new' for My 43 / 51

Mixing in Modules Modules get mixed into classes using include or ext Referred to as "mix-ins" When you mix in a module into a class, that class gets access to all of the methods defined in that module 44 / 51

include vs. ext If you mix-in a module with: include: The module's methods will be available as instance methods ext: The module's methods will be available as class methods 45 / 51

Mixing in Modules module ClassMethodsModule def class_method 'Class Method' module InstanceMethodsModule def instance_method 'Instance Method' class MyClass ext ClassMethodsModule include InstanceMethodsModule p MyClass.class_method #=> "Class Method" p MyClass.new.instance_method #=> "Instance Method" 46 / 51

Separate Module Methods It should be clear if a method is meant to be an instance method or a class method It is extremely unlikely that a method is both Instance and class methods should be separated in the module with more, nested modules 47 / 51

Separate Module Methods module MyModule module InstanceMethods def instance_method p 'Instance Method' module ClassMethods def class_method p 'Class Method' 48 / 51

Using Nested Modules The :: operator can be used to access a module in another module Not to be confused with the documentation syntax of denoting a class method Thus, if a class wanted to access the module in the previous slide class MyClass include MyModule::InstanceMethods ext MyModule::ClassMethods MyClass.new.instance_method #=> "Instance Method" MyClass.class_method #=> "Class Method" 49 / 51

Requiring Files It's good practice for every class and module to have its own file To "import" other files, pass the path to the file as a string without the extension to the require method If the file foo.rb is in the same directory, put require './foo' at the top of the file If it's in the parent directory, require '../foo' Recall that requiring a file without the path imports a gem Note: "requiring" a file loads and runs that file 50 / 51

Homework 2 Homework 2 will be released after class Designed to help you become more comfortable with Object Orientation in Ruby You will be developing a workflow that is very similar to what you'll see when we get to Rails 51 / 51