Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alisonz_solar_system.rb #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#create a class of planet
class Planet
attr_accessor :planet_hash

def initialize (planet_hash)
@name = planet_hash[:name]
@moons = planet_hash[:moons]
@color = planet_hash[:color]
@distance_from_sun = planet_hash[:distance_from_sun]
@rotation = planet_hash[:rotation]
end
#
# def print_info
# planet_hash.each do |planet|
# puts "The planet #{@name} has #{@moons} moons and is #{@color}."
# end
# end
end

#creates hashes of data
earth_hash = {name: "earth", moons: 1, color: "blue/green", distance_from_sun: "92.96 million mi", rotation: "365 days" }
mars_hash = {name: "mars", moons: 2, color: "red", distance_from_sun: "141.6 million miles", rotation: "1.88 years"}
venus_hash = {name: "venus", moons: 0, color: "blue", distance_from_sun:"67.24 million mi", rotation: "224.7 days"}

#empty array to store all planets
planets = []

#create new planet objects with hash info
earth = Planet.new(earth_hash)
mars = Planet.new(mars_hash)
venus = Planet.new(venus_hash)

#store planet instances in the array
planets << earth
planets << mars
planets << venus


class SolarSystem
attr_accessor :name, :planet_array

def initialize (name, planet_array)
@name = name
@planet_array = planet_array
end

#ahhhhhhh!!!!!!! WHAT AM I MISSING???!!!!!!!!!! I AM MISSING A KEY POINT IN THIS LESSON!!!!
#the instance variables are not being read!
def print_planets_info
@planet_array.each do |planet|
puts @planet_array
# puts "This planet is named #{@name}. It is #{@color} and has #{@moons} moons. It is #{@distance_from_sun} and has a solar rotation of approximately #{@rotation}"
# puts @color
# puts "this is the color #{planet.@color}"
end
end


#method to add single planets
def add_planet(new_planet)
@planet_array.push(new_planet)
end

#method to add many planets
def add_planets(new_planets)
@planet_array +=new_planets
end
end

milky_way = SolarSystem.new("milky way", planets)


#not printing out instance variables in solar system, do they need a class in front of them or something?
#does the print_info work in planets
milky_way.print_planets_info