Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harrisonmalone/f1ccbb6ec708355da58091001f09f94a to your computer and use it in GitHub Desktop.
Save harrisonmalone/f1ccbb6ec708355da58091001f09f94a to your computer and use it in GitHub Desktop.
class Continent
attr_reader :countries, :name
@@continents = 0
@@continent_objects = []
def self.num_of_continents
return @@continents
end
def self.print_num_of_continents
return "You have created #{@@continents} #{@@continents < 2 ? "continent" : "continents"}"
end
def self.print_names_of_continent_objects
if @@continents < 1
return "You have no continent objects yet!"
else
@@continent_objects.each_with_index do |continent, index|
return "#{index + 1}) #{continent.name}"
end
end
end
def initialize(name)
@@continents += 1
@name = name
@countries = []
@@continent_objects << self
end
def add_country(country)
@countries << country
end
end
class Country
attr_reader :name, :cities
@@countries = 0
def self.num_of_countries
return @@countries
end
def initialize(name)
@@countries += 1
@name = name
@cities = []
end
def add_cities(city)
@cities << city
end
end
class City
attr_reader :name
@@cities = 0
def self.num_of_cities
return @@cities
end
def initialize(name)
@@cities += 1
@name = name
end
end
# this is the one of the examples that matt likes to see to ensure you understand ruby basics
# it's a big weird object but you see how different classes link together
# creating instances that you then call using instance methods to make an object
# first time properly using class variables, useful to count up the total instances that are made
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment