# Phase3 -- 2. Intro to Object Oriented Ruby
June 29, 2021 - AM
# For Exercise
```rb
episodes = friends["_embedded"]["episodes"]
episode = episodes.first
```
## Key Concepts
### Class
Built in Classes:
- String
- Array
- Hash
Let's create a playground to try out building classes:
1. Create a playground.rb file inside of lib/oo_ruby.
2. open the lib/oo_ruby.rb file and require the playground file.
```rb
# lib/oo_ruby.rb
require_relative "oo_ruby/version"
require_relative "oo_ruby/friends"
require_relative "oo_ruby/episode"
require_relative "oo_ruby/playground"
module OoRuby
class Error < StandardError; end
# Your code goes here...
end
# lib/oo_ruby/playground.rb
puts "hi there!!!"
```
To make sure we're requiring the file correctly, let's run `./bin/console`. We should see hi there!!!
Now, we can write code in playground.rb and play around with it in `./bin/console`
```rb
class Dog
end
```
### Instance
How do we make a new instance of a class?
```
porkchop = Dog.new
```
We get something like this:
```rb
#<Dog:0x00007f8ff0949b48>
```
If we try to give Porkchop a name:
```rb
2.6.6 :004 > porkchop.name = "Porkchop"
Traceback (most recent call last):
2: from ./bin/console:14:in `<main>'
1: from (irb):4
NoMethodError (undefined method `name=' for #<Dog:0x00007f8ff0949b48>)
```
If we were to describe porkchop in technical terms, what would we call porkchop?
An instance of the Dog class. Has a unique identifier. It inherits methods from the class. The class is a blueprint for building instances. An instance shares the properties (instance methods) defined in the class.
Right now, with that in mind, how do we interpret the error above?
If we have a NoMethodError it can be one of 2 problems and there are 3 places we need to look to see what's wrong?
1. name of the method
2. the name of the object receiving the method call
3. the line number where the error occurred
Once you know these 3 things, the problem is one of two things:
1. we called the right method on the wrong object
2. we called the wrong method on the right object
3. We called the right method on the right object but forgot to define it
### Initialize method
If we want to create a Dog with arguments passed to `Dog.new` we need to define an initialize method.
### Getter and setter
Setter methods end with = and take an argument and assign its value to an instance variable.
The syntax for an instance variable consists of prefixing the variable name with the `@`symbol.
```rb
class Dog
#setter method
def name=(name)
@name = name
end
#getter method
def name
@name
end
def age=(age)
@age = age
end
def age
@age
end
def breed=(breed)
@breed = breed
end
def breed
@breed
end
end
```
### Attribute Macros
```rb
class Dog
attr_accessor :name, :age, :breed
end☄
```
### Instance vs Class Methods
### The `self` Keyword
Where are we calling a method? What object are we calling it on? If we don't explicitly invoke a method on an object, what object is the method invoked on?
```rb
class Dog
self.attr_accessor(:name, :age, :breed)
# same as
attr_accessor(:name, :age, :breed)
end
```
If we invoke a method, without explicitly calling it on an object, the implicit receiver is `self`
### Variable Scopes
## Learning Tasks
### Understanding Variable Scope
### Everything in Ruby is An Object – What does this mean?
## Errors
### ArgumentError
```rb
porkchop = Dog.new("Porkchop")
Traceback (most recent call last):
5: from ./bin/console:14:in `<main>'
4: from (irb):8
3: from (irb):8:in `rescue in irb_binding'
2: from (irb):8:in `new'
1: from (irb):8:in `initialize'
ArgumentError (wrong number of arguments (given 1, expected 0))
```
4 pieces of information are crucial:
1. expected number of arguments (how many arguments the method is defined to accept)
2. given number of arguments (how many arguments the method was invoked with)
3. line number where the method is defined ( where the error occurred on)
4. line number where the method was invoked