Skip to content

Instantly share code, notes, and snippets.

@kariabancroft
Created September 29, 2015 16:46
Show Gist options
  • Save kariabancroft/db133c8b56a2b091be4d to your computer and use it in GitHub Desktop.
Save kariabancroft/db133c8b56a2b091be4d to your computer and use it in GitHub Desktop.
# define class
class Planet
attr_accessor :name, :pop_by, :avg_temp, :color, :rate_solar_rotation, :distance_from_sun, :planets, :add_planet, :print_planets
def initialize(name, pop_by, avg_temp, color)
@name = name.capitalize
@pop_by = pop_by
@avg_temp = avg_temp
@color = color
end
def rate_solar_rotation(rate_solar_rotation)
@rate_solar_rotation = rate_solar_rotation
end
def distance_from_sun(distance_from_sun)
@distance_from_sun = distance_from_sun
end
def print_out
puts "#{@name}: What a special planet! It is populated by #{@pop_by} and is the color #{@color}. Its average temperature is #{@avg_temp}."
end
end
# create array - not sure why it needed to be an instance variable to work though?
planets = []
# create instances of class planets
unicorn = Planet.new("unicorn", "unicorns", 1400, "rainbow swirls")
giant = Planet.new("Giant", "very tall people", 70, "brown")
ent = Planet.new("ents", "ents", 99, "green")
# push instances of planets into
planets.push(unicorn)
planets.push(giant)
planets.push(ent)
# define method for requesting user input over
def planets_menu(planet_array)
count = 0
puts "Which planet would you like to learn about? Type the number of your selection:"
planet_array.each do |planet|
puts "#{count + 1}. #{planet.name}"
count += 1
end
puts "#{planet_array.length + 1}. Exit"
result = gets.chomp.to_i
return result
end
# call the method for requesting user input
answer = planets_menu(planets)
# keep calling the method for requesting user input until the last item was selected ("quit")
while answer != (planets.length + 1)
planets[answer - 1].print_out
answer = planets_menu(planets)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment