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

Solar System #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

codeandmorecode
Copy link

Assignment Submission: Solar System

Congratulations! You're submitting your assignment. Please reflect on the assignment with these questions.

Reflection

Question Answer
When does the initialize method run? What does it do? The initialize method runs when we call the class in order to create a new instance of the class in which it lays (i.e. solar_system.rb or planet.rb, from main.rb. The initialize method accepts parameters from the user/main.rb/or the terminal, and creates instance variables within the class file; those are later used within the class methods.
Why do you imagine we made our instance variables readable but not writable? We made our instance variables readable but not writable because we do not need the user to mess around with the code from our classes and possibly cause bugs if the variables are not created as the datatype they need to be, for instance all planets in solar_system.rb should be instances of the Planet class, not some random String or array. I.e., we only need the user to create instances of planets or solar systems.
How would your program be different if each planet was stored as a Hash instead of an instance of a class? I would erase completely the class file planet.rb since we no longer need instances of that class. The instance variables from planet.rb would become the keys and the input would be the values of those keys, in my main.rb file. That is to say, each planet in main.rb would take the following format: mercury = {:name=> "Mercury", :color=> "dark grey", :mass=> 3.28e23, :distance_from_sun=> 6.94e7, :fun_fact=> "Mercury is shrinking!"} My program would be different everywhere I am invoking the @planet, in particular in all of the solar_system class code, since we will no longer invoke an instance of a class, but an array or hash of hashes (the individual planets.
How would your program be different if your SolarSystem class used a Hash instead of an Array to store the list of planets? We will assume that the planets are either instances of the class Planet, or hashes themsleves, as it the datatype for each planet is not specified in this question. The portion @planets = [] from the initialize would be changed to @planets = {}. For list_planets, we would have a new hash instead of a new array; and we would use just map, not map.with_index. The code within that method would be tweaked with keys and values instead of index and item at index. Roughly, in the find_planet method, we would be using keys instead of indexes to retrieve the information. Instead of using the .name on instance_of_planets, we would use the :name key and instead of @planets[index_of_desired_instance], we would have @planet[:name].
There is a software design principle called the SRP. The Single Responsibility Principle (SRP) says that each class should be responsible for exactly one thing. Do your classes follow SRP? What responsibilities do they have? My original classes follow SRP, in that the planet class is responsible to hold all the necessary behaviors and states related information of instances of the class, namely the individual planets; likewise for the solar-system class.
How did you organize your require statements? Which files needed requires, and which did not? What is the pattern? I put my require statements in the first two lines of my main.rb file. The main file needs the requires; the class files do not. The pattern is require_relative "class name".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant