#Day 4
###Object and Classes
groucho = {:yab => 1890, :moustache => true, :vice => "cigars"} harpo = {:yab => 1888, :moustache => false, :vice => "women"}
def how_old(brother) 2014-brother[:yab] end
An object is an instance of a class
class Person end -> Groucho, harpo and chico are objects so instances of the class Person.
classes always start with capital letters.
if we had 2 people to the class Person and then do name.class, it will return "Person" instead of "class".
Methods in class can have the same name in each class, they will return different things.
Function: Block of code with a name that sits on its own Method: needs the parent element to be executed.
Syntactic sugar
def age=(age) @age = age #The value that is passed on, is stored as the variable age. end
p1 = Person.new p1.age=(21) p1.age => 21
we dont have to create 2 methods to set and get the age, we can just use "age'" et set the age using the "=" sign as long as the sign is on the element of our method.
@ = makes the variable available in the whole class, not only in the method.
when using def initialize(age, name, gender) we cannot only do p1 = Person.new, we need to define the variables as well => p1 = Person.new(23,charlie,f)
attr_accessor :age, :name, :gender
When we create a new Person, we can just call "p1./+ tab/" to display all the method that can be applied to the class.
Inheritance "<<"
Person.ancestors displays what the method inheritates from.
####building
Apartment
- Price(monthly : decimal
- sqft: integer
- beds: integer
- bathrooms = integers
- renters :[]
Building:
- address: string
- style: string
- doorman: boolean
- elevator: boolean
- floors: integer
- apartments: [] or {}
Tenant / Person
- name: string
- gender: string
- apartment: (Apartment)
To check if there is something in an array we can do a = [] a.length or a.any?
method that ends with "?" is somehting that had a true/false value.
require_relative 'name of file' finds files in the same folder to attach them to a main file