Lecture 2. Object Orientation

Similar documents
Lecture 2. Object Orientation 1 / 50

Lecture 2. Object Orientation 1 / 51

Lecture 1. Basic Ruby 1 / 61

Lecture 3. Miscellaneous Ruby and Testing 1 / 40

Lecture 4. Ruby on Rails 1 / 52

Ruby: Object-Oriented Concepts

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

CSc 372 Comparative Programming Languages

Lecture 3. Miscellaneous Ruby and Testing

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

CSE 341, Autumn 2015, Ruby Introduction Summary

Lecture 3. Miscellaneous Ruby and Testing 1 / 48

CSE : Python Programming

CSc 372 Comparative Programming Languages

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

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

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

Classes and Modules. Properties. Chapter 11

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

Programming Paradigms

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

CS 105 Perl: Modules and Objects

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

Basic Object-Oriented Concepts. 5-Oct-17

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

MITOCW watch?v=flgjisf3l78

CSCI 161 Introduction to Computer Science

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

CS1210 Lecture 28 Mar. 27, 2019

Java: introduction to object-oriented features

Midterms Save the Dates!

EECS 1001 and EECS 1030M, lab 01 conflict

CSE 341: Programming Languages. Section AC with Nate Yazdani

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

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

15.1 Origins and Uses of Ruby

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

Midterms Save the Dates!

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

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

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

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

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

Lecture 10. Overriding & Casting About

Programming Exercise 14: Inheritance and Polymorphism

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

CS 390 Software Engineering Lecture 6 Ruby

Week 3 Classes and Objects

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Overview of the Ruby Language. By Ron Haley

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Readings for This Lecture

Super-Classes and sub-classes

CS 231 Data Structures and Algorithms, Fall 2016

ITI Introduction to Computing II

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Notes on Chapter Three

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

6.001 Notes: Section 8.1

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Principles of Object Oriented Programming. Lecture 4

CPS 506 Comparative Programming Languages. Programming Language

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

Java Object Oriented Design. CSC207 Fall 2014

1B1b Classes in Java Part I

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 02, Fall 2018 Friday September 7

ITI Introduction to Computing II

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

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Chapter 4 Defining Classes I

Data Structures (list, dictionary, tuples, sets, strings)

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Inheritance and Interfaces

Ruby: Introduction, Basics

Import Statements, Instance Members, and the Default Constructor

Sixth lecture; classes, objects, reference operator.

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

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

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

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

CS 5142 Scripting Languages

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Software Design and Analysis for Engineers

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

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

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

CS 134 Programming Exercise 3:

Chapter 2: Java OOP I

CMSC201 Computer Science I for Majors

file:///users/dave/tmp/rubyadv/html/metaprogramming.html

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

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. Classes as Code Libraries. Classes as Data Structures

Transcription:

Lecture 2 Object Orientation 1

Homework 0 Grades Homework 0 grades were returned earlier this week Any questions? 2

Homework 1 Homework 1 is due tonight at 11:59pm 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 3

Office Hours Weekday Time Location TA Monday 4-5pm Harrison Mezz Sanjana Sarkar 6-7pm Moore 100 Desmond Howard Tuesday 6-7pm Moore 100 Desmond Howard Wednesday 11am-12pm Moore 100 Sanjana Sarkar 4:30-5:30pm Moore 100 Jackie Askins Thursday 6-7pm Moore 100 Jackie Askins **These are pretty much final, but the locations may changes 4

Classes & Objects Classes define clusters of behavior or functionality *Almost* everything in Ruby is an object Every object is an instance of exactly one class You can call methods on almost anything! Because of this, there are no functions in Ruby 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 5

Creating a Class Use the class and keywords and place class s code between them It is convention to use PascalCase when naming classes Instantiate a class with the new keyword class Person person = Person.new p person.class #=> Person 6

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 7

Instance Variables Instance variables allow you to remember state in individual objects 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 If you define an instance variable in one instance method in a class, you can access it in any other instance method in that class 8

Instance Variables Note that it is bad style in Ruby to prefix methods with set or get, we will fix this soon class Person def set_name(name) @name = name def get_name @name person = Person.new person.set_name('jackie') p person.get_name #=> "Jackie" 9

Constructors If a method is named initialize, it will be executed when a class is being initialized class Person def initialize p 'Hi, I am being initialized!' Person.new #=> "Hi, I am being initialized!" 10

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

Reader Methods What we call getters 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 12

Reader Methods class Person def initialize(name) @name = name def name @name person = Person.new('Jackie') person.name #=> "Jackie" 13

Writer Methods What we call setters in other languages are referred to as writer methods in Ruby Used to set the value of a variable in a class Share name with variable being set followed by an equal sign Ruby uses syntactic sugar to allow us to call the method like it s an assignment 14

Writer Methods class Person def initialize(name) @name = name def name @name def name=(name) @name = name person = Person.new('Jackie') person.name = 'Sanjana' p person.name #=> "Sanjana" 15

Writer Methods Note that writer methods can do more than just assignment class Person def name @name def name=(name) name *= 3 @name = name.length > 10? 'Too long' : name person = Person.new person.name = 'Jackie' p person.name #=> "Too long" 16

Attr Readers It turns out that the use of reader methods is pretty common We can use the attr_reader method to generate a reader method class Person attr_reader :name def initialize(name) @name = name person = Person.new('Jackie') p person.name #=> "Jackie" 17

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 = 'Jackie' p person.name #=> "Jackie" 18

Attr Accessors It turns out that we can generate both a reader and writer method all in one line by using an attr_accessor class Person attr_accessor :name person = Person.new person.name = 'Jackie' p person.name #=> "Jackie" 19

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 provided 20

Generating Multiple Methods class Person attr_reader :name, :age, :favorite_color def initialize(name, age, favorite_color) @name = name @age = age @favorite_color = favorite_color person = Person.new('Jackie', 21, 'blue') p person.name #=> "Jackie" p person.age #=> 21 p person.favorite_color #=> "blue" 21

Class Methods A method inted to be called on the class itself (as opposed to an instance) is called a class method Defined like an instance method, but starts with self. self indicates the class It cannot call any instance methods Similar to a static method in Java 22

Class Methods class Person def self.average_life_expectancy '71.5 years' p Person.average_life_expectancy #=> "71.5 years" 23

self This is similar to the this keyword in Java There s always exactly one current object or self The current object is determined by the scope 24

self At the top level: self refers to the default object, main Inside of a class definition: self refers to the class object itself Inside of a class method: self refers to the class object Inside of an instance method: self refers to the instance that the method is being called on 25

Implicit Receiver All methods must have a receiver When calling student.name, student is the receiver for name What does that mean for instance methods called in another instance method? It has an implicit receiver, and in a class, it is self This is important because: An instance method can be called with an implicit receiver It means there are no functions, only methods, in Ruby 26

Method Notation A quick note on method notation ClassName#instance_method Refers to the instance method instance_method in the class ClassName ClassName::class_method Refers to the class method class_method in the class ClassName 27

Private Methods By default, all methods are public But you don't want to be able to access every method defined in a class Methods can be made private with the private keyword In the body of a class, all methods physically under the private method are private 28

Private Methods class Person def say_hello "Hi, I'm #{name}" private def name 'Jackie' person = Person.new p person.say_hello #=> "Hi, I'm Jackie" p person.name #=> NoMethodError: private method `name' called for #<Person: 0x007fc525011ef8> 29

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 30

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 seem harmless, but they re actually pretty bad practice 31

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 32

A 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 obviously bad As a general rule, don t use class variables unless you mean for this to happen 33

A 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 #=> "Antarctica" p Bird.location #=> "Antarctica 34

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 35

Use Class Instance Variables Since classes are themselves objects, they 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 @ Always use class instance variables, NEVER use class variables 36

Use Class Instance Variables class Bird @location = 'EVERYWHERE' def self.location @location class Penguin < Bird @location = 'Antarctica' p Bird.location #=> "EVERYWHERE" p Penguin.location #=> "Antarctica 37

Classes Never Close Existing classes, including core classes, can be modified at any time just be declaring them my_string = 'hi' my_string.repeat(3) #=> NoMethodError class String def repeat(x) self * x p my_string.repeat(3) #=> "hihihi" This is dangerous and you should never do it unless you really know what you re doing 38

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 Declared with the module keyword Unlike classes, modules cannot be instantiated 39

Modules module MyModule def my_module_method 'Hello World' MyModule.new #=> NoMethodError 40

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

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 42

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" 43

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 module MyModule module InstanceMethods def instance_method puts 'Instance Method' module ClassMethods def class_method puts 'Class Method' 44

Using Nested Modules The :: operator can be used to access a module in another module 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" 45

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 without the extension as a string 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 46

Homework 2 Homework 2 is released, it is designed to help you become more comfortable with Object Orientation You will be developing a workflow that you will see when we get to Rails https://classroom.github.com/a/o4dyd9sl 47

Homework 2 Passing a hash as an argument to a method def method_name(params) @foo = params[:foo] @bar = params[:bar] @baz = params[:baz] 'Assigned foo, bar, and baz from params hash' p method_name(foo: 'foo', bar: 'bar', baz: 'baz') #=> "Assigned foo, bar, and baz from params hash" https://classroom.github.com/a/o4dyd9sl 48