Study Guide
120 - Object Oriented Programming
classes and objects
- classes is the template while object is the object created out of the template.
- class are focus on two things , states and behaviors.
Object from the same class will have the same methods, same number of variables and variable names. However, the actual value of the variables will be different. These are object's states.
- classes is the template while object is the object created out of the template.
- class are focus on two things , states and behaviors.
Object from the same class will have the same methods, same number of variables and variable names. However, the actual value of the variables will be different. These are object's states.
using attr_* to automatically create setter and/or getter methods, and how to call them
attr_accessor - create both a setter and getter of instance variable
attr_reader - create just a getter
attr_writer - create just a setter.
- all attr* will create an instance variable for you, initialize to nil.
- attr_accessor :name - this will create setter and getter for the instance var for @name . It will find the ivar with the same name.
- when using setter method. Be sure to use "self.setter_method" syntax , if not , the computer think you are initializing a local variable.
- for getter method, self is not required.
instance variable vs class variable vs constant
instance variable
-instance variable starts with "@" and live within the object's class. It lives on until the object is terminated.
-there is no way to obtain or set the value of instance variable unless through a getter or setter method.
-if you reference an instance variable without initializing it, it is set to nil (no error given)
class variables
- starts with double "@@".
- class var is available throughout the class and only holds 1 copy.
- even subclass will share 1 copy of class variable. So if subclass modify class variables, all other subclass and parent class will have the modified class variables.
constant
- upcase.
- constant is available throughout the class.
- if u want to refer to another constant from another class, use this syntax ClassName::CONSTANT
attr_accessor - create both a setter and getter of instance variable
attr_reader - create just a getter
attr_writer - create just a setter.
- all attr* will create an instance variable for you, initialize to nil.
- attr_accessor :name - this will create setter and getter for the instance var for @name . It will find the ivar with the same name.
- when using setter method. Be sure to use "self.setter_method" syntax , if not , the computer think you are initializing a local variable.
- for getter method, self is not required.
instance variable vs class variable vs constant
instance variable
-instance variable starts with "@" and live within the object's class. It lives on until the object is terminated.
-there is no way to obtain or set the value of instance variable unless through a getter or setter method.
-if you reference an instance variable without initializing it, it is set to nil (no error given)
class variables
- starts with double "@@".
- class var is available throughout the class and only holds 1 copy.
- even subclass will share 1 copy of class variable. So if subclass modify class variables, all other subclass and parent class will have the modified class variables.
constant
- upcase.
- constant is available throughout the class.
- if u want to refer to another constant from another class, use this syntax ClassName::CONSTANT
instance methods vs class methods
- instance methods :
def methods
# method implementation.
end
- you can retrieve class variable with instance method.
- instance method available to objects only.
Eg:
dog = GoodDog.new
dog.method
-class method available to class only
- you can't retrieve instance var or call instance method with class method.
- class methods :
def self.methods
# method implementation.
end
Eg:
GoodDog.method
- instance methods :
def methods
# method implementation.
end
- you can retrieve class variable with instance method.
- instance method available to objects only.
Eg:
dog = GoodDog.new
dog.method
-class method available to class only
- you can't retrieve instance var or call instance method with class method.
- class methods :
def self.methods
# method implementation.
end
Eg:
GoodDog.method
referencing and setting an instance variable vs using getter/setter methods
if you reference or set an instance var directly, you may not enjoy the pre-processing available in a typical getter/setter methods.
if you reference or set an instance var directly, you may not enjoy the pre-processing available in a typical getter/setter methods.
class inheritance
- this is when a class inherit behavior from another class
- The syntax as follow: ClassName is the subclass, SuperClass is the superclass.
class ClassName < SuperClass
end
- when you have a method with the same name in subclass and superclass , subclass's method will be called because ruby will search inside the current class first for the method. It follows the class.ancestor hierarchy
super
- if you call "super" inside a subclass, the method of the same name in superclass will be called.
- if you omit any argument, all argument from subclass will be implicitly passed to super
- if you called super(), then no argument is passed .
- if you called super(arg1, arg2) then arg1 and arg2 will be passed and so on.
class Animal attr_accessor :name def initialize(name) @name = name end end
class BadDog < Animal def initialize(age, name) super(name) @age = age end end BadDog.new(2, "bear")
Here, @name will be "bear" and @age is 2.
module
- this is when a class inherit behavior from another class
- The syntax as follow: ClassName is the subclass, SuperClass is the superclass.
class ClassName < SuperClass
end
- when you have a method with the same name in subclass and superclass , subclass's method will be called because ruby will search inside the current class first for the method. It follows the class.ancestor hierarchy
super
- if you call "super" inside a subclass, the method of the same name in superclass will be called.
- if you omit any argument, all argument from subclass will be implicitly passed to super
- if you called super(), then no argument is passed .
- if you called super(arg1, arg2) then arg1 and arg2 will be passed and so on.
class Animal attr_accessor :name def initialize(name) @name = name end end
class BadDog < Animal def initialize(age, name) super(name) @age = age end end BadDog.new(2, "bear")
Here, @name will be "bear" and @age is 2.
module
- a module allow us to group a reusable chunk of code.
- use #include keyword to pass in the module.
- no object can be instantied from a module.
-can be used for namespacing , ie grouping related class together.
- use as container for some method.
- use #include keyword to pass in the module.
- no object can be instantied from a module.
-can be used for namespacing , ie grouping related class together.
- use as container for some method.
method lookup path
-using the ancestors method call on Class name
-method override will happen if subclass method's name is the same as parent class. The order of importance is based on the method lookup path.
-ruby will search for method calls in the following orders, the object’s class, mixin modules, superclass, superclass's modules, Object, Kernel , BasicObject. Ruby will search the last included mixin first, then go up the list of modules.
-It will stop when it found the method.
Example:
class Animal include Walkable def speak "I'm an animal, and I speak!" end end
class Animal include Walkable def speak "I'm an animal, and I speak!" end end
class GoodDog < Animal
include Swimmable
include Climbable
end
puts "---GoodDog method lookup---"
puts GoodDog.ancestors
---GoodDog method lookup---
GoodDog
Climbable # last mixin search first
Swimmable
Animal # superclass
Walkable # module in superclass
Object
Kernel
BasicObject
self
- self , when called outside of instance method , is actually the class.
- if called inside instance method, it is the object.
- self , when called outside of instance method , is actually the class.
- if called inside instance method, it is the object.
calling methods with self
- if you call a method like this :
def self.method
"hello world"
end
this is a class method. Note: the self here is the class.
-if you call a method like this:
def method
self.another_method
end
it is calling instance method and is not required. Note : the self here is the object
- if you call a method like this :
def self.method
"hello world"
end
this is a class method. Note: the self here is the class.
-if you call a method like this:
def method
self.another_method
end
it is calling instance method and is not required. Note : the self here is the object
more about self
def self.method
end
is the same as
def ClassName.method
end
def self.method
end
is the same as
def ClassName.method
end
being able to read OO code
fake operators
Certain symbols looked like operators but actually methods . These methods you can override in your custom class.
Certain symbols looked like operators but actually methods . These methods you can override in your custom class.
truthiness
-short_circuit
&& , will stop checking when one of the statement is false.
| | , will stop checking if one of the statement is true.
-short_circuit
&& , will stop checking when one of the statement is false.
| | , will stop checking if one of the statement is true.
working with collaborator objects
You can pass in custom objects into another custom object.
Once the object is in your custom object, both is said to be collaborating with each other.
private, public and protected
To define private, public or protected method, use the name itself as keywords and anything below it will be that type of method.
-public methods can be called anywhere
private vs protected:
-private - can only be reference inside the class definition. Also, can't be reference inside class definition with "self.private_method" because self is an object. It is that private.
-protected - a little relaxed. It can't be reference outside the class definition. However, inside class definition , can be reference with "self.protected_method".
Vocab
1. Instantiation - creating of a new object out from a class.
2. Encapsulation - hiding pieces of functionality and making it unavailable to the rest of the code base.
You can pass in custom objects into another custom object.
Once the object is in your custom object, both is said to be collaborating with each other.
private, public and protected
To define private, public or protected method, use the name itself as keywords and anything below it will be that type of method.
-public methods can be called anywhere
private vs protected:
-private - can only be reference inside the class definition. Also, can't be reference inside class definition with "self.private_method" because self is an object. It is that private.
-protected - a little relaxed. It can't be reference outside the class definition. However, inside class definition , can be reference with "self.protected_method".
Vocab
1. Instantiation - creating of a new object out from a class.
2. Encapsulation - hiding pieces of functionality and making it unavailable to the rest of the code base.
130 - Ruby Foundations: More Topics
how blocks work, and when we'd want to use them
- block is just an implicit argument pass into a method. A block is a chunk of code like a method.
- block doesn't care about number of arguments passed in.
- yield keyword means to execute the block. The last line of the block is the return value of the block by default
- if you dont provide a block but call yield keyword, localjumperror will be raised.
- use "block_given?" keyword to check if a block is written.
- unintialized block argument will be nil.
- block is just an implicit argument pass into a method. A block is a chunk of code like a method.
- block doesn't care about number of arguments passed in.
- yield keyword means to execute the block. The last line of the block is the return value of the block by default
- if you dont provide a block but call yield keyword, localjumperror will be raised.
- use "block_given?" keyword to check if a block is written.
- unintialized block argument will be nil.
how to implement your own each, select, reduce, and map in a custom collection-oriented class
- mostly using while, until and loop iterations.
- mostly using while, until and loop iterations.
Comments
Post a Comment