Created
April 26, 2018 10:33
-
-
Save rikas/277a35b4d28f307dabf7bc468ee56771 to your computer and use it in GitHub Desktop.
Inheritance & Self
This file contains 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
# chef.rb | |
class Chef | |
attr_reader :name, :restaurant | |
def initialize(name, restaurant) | |
@name = name # String | |
@restaurant = restaurant # Restaurant | |
end | |
end |
This file contains 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
# fancy_restaurant.rb | |
require_relative 'restaurant' | |
class FancyRestaurant < Restaurant | |
def initialize(name, city, capacity, category, stars) | |
# DATA (instance variables) | |
# @name = name | |
# @city = city | |
# @capacity = capacity | |
# @category = category | |
# @clients = [] | |
# it calls initialize from Restaurant | |
super(name, city, capacity, category) | |
@stars = stars | |
end | |
def print_clients | |
puts "[!] FORBIDDEN! GO AWAY!" | |
end | |
end |
This file contains 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
# fast_food_restaurant.rb | |
require_relative 'restaurant' | |
class FastFoodRestaurant < Restaurant | |
def initialize(name, city, capacity, category, prep_time) | |
# DATA (instance variables) | |
# @name = name | |
# @city = city | |
# @capacity = capacity | |
# @category = category | |
# @clients = [] | |
# it calls initialize from Restaurant | |
super(name, city, capacity, category) | |
@prep_time = prep_time | |
end | |
def open? | |
super || Time.now.hour == 13 | |
end | |
end |
This file contains 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
# inheritance_test.rb | |
require_relative 'restaurant' | |
require_relative 'fast_food_restaurant' | |
require_relative 'fancy_restaurant' | |
# create an instance of FastFoodRestaurant | |
# name, city, capacity, category, prep_time | |
burger_king = FastFoodRestaurant.new( | |
'Burger King', 'Lisbon', 120, 'burgers', 5) | |
belcanto = FancyRestaurant.new( | |
'Belcanto', 'Lisbon', 50, 'experimental', 2) | |
puts "#{burger_king.name} is in #{burger_king.city}" | |
puts "#{belcanto.name} is in #{belcanto.city}" | |
burger_king.book('Maria') | |
burger_king.book('Ricardo') | |
burger_king.print_clients | |
belcanto.book('Maria') | |
belcanto.print_clients | |
# class method | |
puts Restaurant.categories | |
# puts belcanto.categories # this doesn't work! |
This file contains 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
# restaurant.rb | |
require_relative 'chef' | |
class Restaurant | |
attr_reader :name, :city, :chef #, :capacity | |
#attr_writer :capacity | |
attr_accessor :capacity | |
# This is the initializer to be used when testing inheritance! | |
def initialize(name, city, capacity, category) | |
# DATA (instance variables) | |
@name = name | |
@city = city | |
@capacity = capacity | |
@category = category | |
@clients = [] | |
end | |
# This is the initializer to be used when testing self | |
# def initialize(name, city, capacity, category, chef_name) | |
# # DATA (instance variables) | |
# @name = name | |
# @city = city | |
# @capacity = capacity | |
# @category = category | |
# @clients = [] | |
# | |
# # self is the current instance | |
# @chef = Chef.new(chef_name, self) | |
# end | |
# self is the current Class (Restaurant) | |
def self.categories | |
%w[seafood sushi burgers] | |
end | |
# BEHAVIOUR (instance methods) | |
def book(client_name) | |
@clients << client_name | |
end | |
def open? | |
Time.now.hour >= 18 && Time.now.hour <= 22 | |
end | |
def closed? | |
!open? | |
# Time.now.hour < 18 || Time.now.hour > 22 | |
end | |
def print_clients | |
puts "Here are the clients of #{@name}" | |
@clients.each do |client| | |
puts "- #{client}" | |
end | |
end | |
# def add_seats(seats) | |
# @capacity += seats | |
# end | |
# the same as att_reader :name | |
# def name | |
# @name | |
# end | |
# def capacity=(new_capacity) | |
# @capacity = new_capacity | |
# end | |
end |
This file contains 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
# restauran_test.rb | |
require_relative 'restaurant' | |
# create a new Restaurant instance | |
# name, city, capacity, category | |
sushi_shop = Restaurant.new( | |
'Sushi Shop', 'Lisbon', 100, 'sushi', 'Akira') | |
# p sushi_shop | |
puts "#{sushi_shop.name} is in #{sushi_shop.city}" | |
# Construction work... | |
sushi_shop.capacity = 100 | |
puts "It now has #{sushi_shop.capacity} seats" | |
puts "#{sushi_shop.name} is #{sushi_shop.open? ? 'open' : 'closed'}" | |
#p sushi_shop | |
sushi_shop.book('Joana') | |
sushi_shop.book('Maria') | |
# sushi_shop.clients = [] | |
new_chef = Chef.new('Pablo', sushi_shop) | |
p new_chef | |
#p sushi_shop | |
akira = sushi_shop.chef # Chef instance | |
#puts "#{sushi_shop.name} chef name is #{akira.name}" | |
#puts sushi_shop.chef.restaurant.chef.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment