Created
November 1, 2018 14:21
-
-
Save rodloboz/927bd0aa2b78b6c20ae0a99ec1f52d0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'person' | |
# ARRAY CRUD | |
# CREATE | |
# index. 0. 1. ... | |
musicians = ["paul", "ringo"] | |
p musicians | |
# READ | |
puts musicians[0].capitalize | |
# UPDATE | |
musicians[1] = "john" | |
p musicians | |
musicians << "ringo" | |
p musicians | |
# DELETE | |
# delete_at method receives an index | |
musicians.delete_at(2) | |
p musicians | |
musicians[0] = nil | |
p musicians | |
numbers = [1, 2, 4, 2, 2, 6, 3] | |
p numbers | |
numbers.delete(2) | |
p numbers | |
# HASH CRUD | |
# CREATE | |
person = { name: "rui" } | |
p person | |
# READ | |
# string | |
puts "Hi, #{person[:name].capitalize}" | |
# UPDATE | |
person[:name] = "john" | |
p person | |
# creating a new key/value pair | |
person[:country] = "Portugal" | |
p person | |
# DELETE | |
person.delete(:country) | |
p person | |
# CLASSES | |
# with hash | |
puts "Hi, #{person[:name].capitalize}" | |
rui = Person.new({name: "Rui"}) | |
# with class | |
# rui is an instance of Person | |
puts rui.class | |
# i need to call it on the instance | |
rui.greet | |
# use a class method | |
# I need to call it on the class | |
puts Person.props |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
def initialize(attributes = {}) | |
# define instance variables | |
@name = attributes[:name] | |
end | |
# instance variable ara available | |
# across the class | |
def greet # instance method | |
puts "Hi, #{@name}" | |
end | |
def self.props # class method | |
"Hi, my properties are name" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment