1

I would like users to be able to dynamically create objects of the Incomes class below. That is, I would like to fire my program and let users enter as many incomes as they like, all stored as instances of the Incomes class.

def prompt
puts "> "
end

class Incomes
def initialize(aName, aAmount, aCOLA)
@name = aName
@amount = aAmount
@COLA = aCOLA
end
end

def addIncome
puts "What is the company name?"
prompt
aName = gets.chomp
puts "What is the monthly amount?"
aAmount = gets.chomp
puts "What is the cost of living adjustment?"
aCOLA = gets.chomp
end
#Now I want to be able to loop back through addIncome and create as many objects as the
#user wants. Perhaps there's a better way to store this type of data?
6
  • Post your code, probably better on SE Code Review Commented Jul 11, 2013 at 20:04
  • Why SE? I find most of the answers I need on stackoverflow. Commented Jul 11, 2013 at 20:12
  • 1
    This is SE. Why Code Review? Because you will be able to get refactored code and theory behind it. You may find most of the answers you need on SO, but that is irrelevant when the kind of question you ask changes. Commented Jul 11, 2013 at 20:21
  • But you are asking to create classes based on user input, with the attributes given. You may want to create Struct instead. This comes from Standard Library. It is designed for simple objects and it is "Hash-like" as well. Commented Jul 11, 2013 at 20:38
  • I don't know structs, but ruby-doc said they bundle together attributes. I'm going to add functions to the class as well so I'd rather go with a class. Also I don't see anything on dynamically creating objects with structs. I don't need subclasses, they will all share the same three attributes. Commented Jul 11, 2013 at 21:03

1 Answer 1

1
def prompt question
  print "#{question} > "
  gets
end

class Incomes
  attr_reader :name, :amount, :COLA
  @@instances_of_Incomes = Array.new
  def initialize(aName, aAmount, aCOLA)
    @name = aName
    @amount = aAmount
    @COLA = aCOLA
    @instances_of_Incomes = Array.new
  end

  def self.addIncome
    name = prompt "What is the company name?"
    amount = prompt "What is the monthly amount?"
    _COLA = prompt "What is the cost of living adjustment?"
    @@instances_of_Incomes << Incomes.new(name, amount, _COLA)
  end

  def self.instances
    @@instances_of_Incomes
  end
end

5.times do
  Incomes.addIncome
end
puts Incomes.instances
Incomes.instances.each do |company|
  puts company.name
end

I have refactored the code to show that you can use inputs to create the instances. They are unnamed classes, but stored in a class variable.

I also show that you can extract the name of each Incomes instance.

I have also edited your SE Code Review question, with the same code, so hopefully you can get some good reviews.

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

2 Comments

I used your style of naming, rather than the Ruby conventions of method names using underscored_names rather than camelCase names.
I don't know if this answer should be accepted or not, as it is not necessarily the best way to do this. But it does answer the question that you can. But you may edit your question on Code Review and use this code in your question and hopefully get better reviews there. (Mine got denied on your "on hold" question there.)

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.