Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created November 1, 2018 14:21
Show Gist options
  • Save rodloboz/927bd0aa2b78b6c20ae0a99ec1f52d0f to your computer and use it in GitHub Desktop.
Save rodloboz/927bd0aa2b78b6c20ae0a99ec1f52d0f to your computer and use it in GitHub Desktop.
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
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