2

I have created a small Ruby class here:

class Star

#Star initialization
def initialize(star, number)
    @star = star
    @number = number
end
end

and I am looking to initialize a class called Solar System with 100 stars. This is what I have done and it doesn't seem to be working. Any help would be greatly appreciated.

require_relative 'star.rb'

class SolarSystem

#Initialize Game
def initialize(partOfSolarSystem)
    @partOfSolarSystem = partOfSolarSystem
    @stars_array = []
    for i in 0..99
        stars_array = Star.new('unknown_star',i)
    end
end

def show_solar_system

    #code here to show all the initialized stars in solar system

end

end

I can't seem to get it to initialize the array in the constructor. I would then like to be able to print out all of the elements in the stars array. Any help with this would be greatly appreciated.

Also in an effort to eventually move this to a database with rails or something of that nature, should I be looking to hash this or will this be easily converted to mySQL or another DB with some helper functions? I would eventually like to write this into rails and have a dynamic website for it.

Once again, thanks very much.

1
  • Usually 'solar system' have only one star - it is named "Sun" :) Maybe there is a better model name, like "Star cluster"? Commented Dec 24, 2013 at 4:51

3 Answers 3

3

Your problem is assigning a new value to @stars_array variable on each iteration. There are multiple ways to deal with it:

@stars_array = (0..99).map { |i| Star.new('unknown_star',i) }

By the way, there is a couple of design issues (just for your attention):

  1. Why variable is called stars_array, not just stars?

  2. Why would ever instance of Star class have some object named @star inside? Recursion? :) Seems like @name would be proper and more clear attribute's name.

  3. Don't miss indentation.


EDIT: About DB-mapping. Most common way - inherit both classes from ActiveRecord::Base, and create one-to-many relation from solar system to stars. Each class will have it's own table. Takes absolutely no efforts.

Sign up to request clarification or add additional context in comments.

Comments

1

You are assigning the new object every time round the loop. The fix is to append the new object:

@stars_array << Star.new('unknown_star',i)

Or, if you prefer words rather than symbols:

@stars_array.push(Star.new('unknown_star',i))

Or, to be more terse:

100.times {|i| @stars_array << Star.new('unknown_star',i) }

Comments

0

A few things to fix to make it work. In your loop you're assigning a new value to the array rather than appending to it. Secondly, in your loop you're using a local variable stars_array instead of the instance variable @stars_array.

Your initialize method should look like this:

def initialize(part_of_solar_system)
  @part_of_solar_system = part_of_solar_system
  @stars_array = []
  for i in 0..99
    @stars_array << Star.new('unknown_star', i)
  end
end

Also, you might want to revisit your Ruby idioms, like preferring snake_case to camelCase for variable names and avoiding for loops in favor of each, e.g.

def initialize(part_of_solar_system)
  @part_of_solar_system = part_of_solar_system
  @stars_array = []
  (0..99).each { |i| @stars_array << Star.new('unknown_star', i) }
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.