Created
September 4, 2015 18:00
-
-
Save mayfer/3d9b13ad3e9d0be3c502 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
pizza_crust = "thin" | |
pizza_cheese = "super cheezy" | |
# so on | |
### vs ### | |
pizza = { | |
crust: "thin", | |
cheese: "super cheesy", | |
toppings: "none", | |
} | |
pizza.each do |attr, value| | |
puts "This pizza has #{value} #{attr}." | |
end | |
################# | |
pizza_shop = { | |
ovens: [ | |
"front", | |
"back", | |
], | |
employees: [ | |
{ | |
name: "sue", | |
}, | |
{ | |
name: "booboos", | |
} | |
], | |
objective: "$$$", | |
} | |
def show_employees(shop) | |
shop[:employees].each do |employee| | |
puts employee[:name] | |
end | |
end | |
show_employees(pizza_shop) | |
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 Employee | |
attr_reader :name | |
def initialize(name) | |
if name_allowed?(name) | |
@name = name | |
else | |
# censor | |
@name = "Bleep" | |
end | |
end | |
# public (getter) instance method | |
def name | |
return @name | |
end | |
# class method that returns an instance! | |
def self.generate_random | |
name = ('a'..'z').to_a.shuffle[0..7].join.capitalize | |
return Employee.new(name) | |
end | |
# private instance method | |
private | |
def name_allowed?(name) | |
banned_names = ["Moon Unit", "Dweezil"] | |
return !banned_names.include?(name) | |
end | |
end | |
em = Employee.generate_random | |
puts em.inspect | |
puts | |
puts | |
class Shop | |
attr_accessor :name, :location | |
@@num_shops = 0 | |
# instance method | |
def initialize(name, employees) | |
# instance variable | |
@name = name | |
@employees = employees | |
@@num_shops += 1 | |
end | |
# instance method | |
def show_employees | |
@employees.each do |employee| | |
puts employee.name | |
end | |
end | |
# class method | |
def self.num_shops | |
@@num_shops | |
end | |
# aand an instance method, accessing a class variable... | |
def num_shops | |
@@num_shops | |
end | |
end | |
# Class inheritance | |
# child # parent | |
class PizzaShop < Shop | |
attr_accesor :ovens | |
def initialize(name, employees, ovens) | |
super(name, employees) | |
@ovens = ovens | |
end | |
end | |
puts "num shops, before any shops, is #{Shop.num_shops}" | |
employee1 = Employee.new("Jonny") | |
employee2 = Employee.new("Dweezil") | |
employees = [employee1, employee2] | |
shop1 = Shop.new("Big jonny", employees) | |
puts "Name is #{shop1.name}" | |
shop1.name = "Lil joni" | |
puts "Name is #{shop1.name}" | |
shop1.location = "just somewhere idk" | |
puts "shop's at #{shop1.location}" | |
puts "shop has employees:" | |
shop1.show_employees | |
puts | |
puts "Num shops: #{Shop.num_shops}" | |
shop1 = Shop.new("Shoe shop", []) | |
puts | |
puts "Num shops: #{Shop.num_shops}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment