Lecture 2 Object Orientation 1 / 50
Homework 1 Homework 1 was due last night 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 Monday 2 / 50
Tentative O ce Hours Weekday Time Location TA Sunday 11am-12pm Harrison Mezz Sanjana 2-3pm Harnwell Mezz Desmond Monday 5:30-6:30pm Moore 100 Sanjana Tuesday 3-4pm Moore 100 Zhilei 5-6pm Moore 100 Desmond 6-7pm Moore 100 Jackie Wednesday 7-8pm Moore 100 Jackie Thursday 3:30-4:30pm Moore 100 Zhilei 3 / 50
CIS 19x Lecture Tonight's shared CIS 19x lecture will cover Git & Version Control If you didn't understand what you were doing when you submitted homework 1, you should att this lecture 4 / 50
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 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. 5 / 50
Object Orientation 6 / 50
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 7 / 50
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 8 / 50
Constructors If you define a method named initialize, it will be executed when creating an instance of the class class Person def initialize p 'Hi, I am being initialized!' Person.new #=> "Hi, I am being initialized!" 9 / 50
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('Jackie') #=> "You've initialized a person with name: Jackie" 10 / 50
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 class Person def initialize(name) @name = name p "Assigned instance variable: #{@name}" Person.new('Jackie') #=> "Assigned instance variable: Jackie" 11 / 50
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" 12 / 50
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 13 / 50
Instance Methods & Instance Variables class Person def set_name(name) @name = name def get_name @name person = Person.new person.set_name('jackie') p person.get_name #=> "Jackie" Note that the above is actually poor style in Ruby 14 / 50
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 15 / 50
Reader Methods class Person def initialize(name) @name = name def name @name person = Person.new('Jackie') p person.name #=> "Jackie" 16 / 50
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 the class Share name with variable being set followed by = Ruby uses syntactic sugar to allow us to call the method like it's an assignment 17 / 50
Writer Methods class Person def initialize(name) @name = name def name @name def name=(name) @name = name person = Person.new('Jackie') person.name = 'Jacqueline' p person.name #=> "Jacqueline" 18 / 50
Writer Methods Note that writer methods can do more than just simple 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" 19 / 50
Attr Readers The use of reader methods in Ruby 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" 20 / 50
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" 21 / 50
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 = 'Jackie' p person.name #=> "Jackie" 22 / 50
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 23 / 50
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('Jackie', 21, 'Ruby') p person.name #=> "Jackie" p person.age #=> 21 p person.favorite_language #=> "Ruby" 24 / 50
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. It cannot call any instance methods Similar to a static method in Java class Person def self.average_life_expectancy '71.5 years' p Person.average_life_expectancy #=> "71.5 years" 25 / 50
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 26 / 50
self At the top level (outside of any class): self corresponds to main (the default object) Inside of a class definition: self corresponds to the class object itself Inside of a class method: self corresponds to the class object Inside of an instance method: self corresponds to the instance that the method is being called on 27 / 50
Implicit Receiver All methods must have a receiver (i.e. all methods must be called on an object) When calling student.name, student is the receiver for the name method But what about when you call an instance method within another instance method without specifying an object? It has an implicit receiver, self This is important because it means that there are no functions, only methods, in Ruby 28 / 50
Describing Methods If I want to convey that a method is an instance method, I will describe it as: ClassName#instance_method (i.e. String#capitalize) If I want to convey that a method is a class method, I will describe it as: 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 29 / 50
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 30 / 50
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 31 / 50
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 32 / 50
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 33 / 50
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 34 / 50
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 35 / 50
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" 36 / 50
How de 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 37 / 50
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 38 / 50
Use Class Instance Variables Always use class instance variables, NEVER use class variables class Bird @location = 'EVERYWHERE' def self.location @location class Penguin < Bird @location = 'Antarctica' p Bird.location #=> "EVERYWHERE" p Penguin.location #=> "Antarctica" 39 / 50
Classes Never Close Existing classes, including core classes, can be modified at any time just by declaring them my_string = 'hi' my_string.repeat(3) #=> NoMethodError: undefined method `repe 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 40 / 50
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 41 / 50
Modules module MyModule def my_module_method 'Hello World' MyModule.new #=> NoMethodError: undefined method `new' for My 42 / 50
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 43 / 50
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 44 / 50
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" 45 / 50
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 46 / 50
Separate Module Methods module MyModule module InstanceMethods def instance_method p 'Instance Method' module ClassMethods def class_method p 'Class Method' 47 / 50
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" 48 / 50
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 49 / 50
Homework 2 Homework 2 is released, it is 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 50 / 50