Skip to content

Instantly share code, notes, and snippets.

@livmaria7891
Created August 17, 2016 04:50
Show Gist options
  • Save livmaria7891/a86312bbd015ac14b226f73e42967296 to your computer and use it in GitHub Desktop.
Save livmaria7891/a86312bbd015ac14b226f73e42967296 to your computer and use it in GitHub Desktop.
Solar System Wave 2
class Planet
attr_accessor :planet_hash, :name, :moons, :escape_v, :surface_temp, :type, :distance
def initialize(planet_hash)
@name = planet_hash[:name]
@moons = planet_hash[:moons]
@escape_v = planet_hash[:escape_v]
@surface_temp = planet_hash[:temp]
@type = planet_hash[:type]
@distance = planet_hash[:distance]
end
def tell_facts
puts "\n#{@name} is a #{@type} planet!\n\nNumber of moons: #{@moons}\nAverage Surface Temperature: #{@surface_temp}\nDistance from the Sun: #{@distance_from_the_sun}\nEscape Velocity: #{@escape_v}\n\n\n"
end
end
class SolarSystem
def planets
@planets
end
#Takes string or array of planet names
def initialize(planets = [])
@planets = [planets]
@planets.flatten!
end
def add_planets(new_planets)
@planets.push(new_planets).flatten!
print @planets
end
def find_distance(planet1,planet2)
(planet1.distance - planet2.distance).abs
end
end
#Data for each planet
mercury = Planet.new({name:"Mercury",moons:0,escape_v:4300,temp:452, type:"rocky",distance:57909175})
venus = Planet.new({name:"Venus",moons:0,escape_v:10400,temp:726,type:"rocky",distance:108208930})
earth = Planet.new({name:"Earth",moons:1,escape_v:11200,temp:281,type:"rocky",distance:149597890})
mars = Planet.new{name:"Mars",moons:2,escape_v:5000,temp:218,type:"rocky",distance:227936640}
jupiter = Planet.new{name:"Jupiter",moons:67,escape_v:59500,temp:120,type:"gaseous",distance:778412020}
saturn = Planet.new{name:"Saturn",moons:62,escape_v:35600,temp:88,type:"gaseous",distance:886715600}
uranus = Planet.new{name:"Uranus",moons:27,escape_v:21300,temp:59,type:"gaseous",distance:2870972200}
neptune = Planet.new{name:"Neptune",moons:14,escape_v:23300,temp:48,type:"gaseous",distance:4498252900}
milkyway = SolarSystem.new([mercury,venus])
puts milkyway.find_distance(mercury,venus)
#Optional Enhancement for USER INPUT
puts "Here are the planets:\n1. #{mercury.name}\n2. #{venus.name}\n3. #{earth.name}\n4. #{mars.name}\n5. #{jupiter.name}\n6. #{saturn.name}\n7. #{uranus.name}\n8. #{neptune.name}\n"
go = true
while go
print "Type a planet's name to learn more about it, or type exit when you're finished: "
choice = gets.chomp.upcase
case choice
when "MERCURY"
puts mercury.tell_facts
when "VENUS"
puts venus.tell_facts
when "EARTH"
puts earth.tell_facts
when "MARS"
puts mars.tell_facts
when "JUPITER"
puts jupiter.tell_facts
when "SATURN"
puts saturn.tell_facts
when "URANUS"
puts uranus.tell_facts
when "NEPTUNE"
puts neptune.tell_facts
when "EXIT"
go = false
end
end
@CheezItMan
Copy link

CheezItMan commented Aug 17, 2016

Cleanly written and well done, but do you need this in the initialize method:

@planets = [planets]
@planets.flatten!

could it be simpler?

@livmaria7891
Copy link
Author

For Wave 2, I accomplished the learning goals. I showed that I could use an array to store a list of objects. I figured out how to combine an array of objects and individual objects into a single, one dimensional array. I also successfully wrote three methods in the class. Overall, I was pleased with the quality of the code but I learned that I could have written the code block inside the initialize method in a single line of code. It also looks like I also left an unnecessary line of code in the add_planets method. Besides those two things, I’m not sure at this point in time how I could have improved on what I wrote, but I would like to know more about deciding how many and which kind of methods belong in a single class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment