Code Snippet to store personal data for later use? How to do better/shorter?

Hi guys,
I’m new with Ruby and try to improve my coding skills. My way works but I’m sure it’s not the best and certainly not the fanciest way to do it.

I was hoping to get some expertise here how to do it faster/shorter/better/smarter…It is really hard to improve without knowing your mistakes.

What the code does: gets name, age, origin of three people one by one by calling a method. Finally stores that data in an array for later use

Full code snippet: What can I improve? How to do it smarter/shorter/better?

# Define a person with name, age, origin
class Person
  def input(count) 
    puts "Hi, let\'s create three new Person. Start with number #{count}"
    puts "What\'s your name?"
    @name = gets
    @name = @name.delete("\n")
    puts "How old are you?"
    @age = gets
    @age = @age.delete("\n")
    puts "Where are you from?"
    @origin = gets
    @origin = @origin.delete("\n")
    puts "I summarize! You are #{@name} and #{@age} years old from #{@origin}"
    puts "Is that correct? \nEnter \"0=Yeah\" or \"Nah\""
    @antwort = gets
    if @antwort.to_i == 0
      puts "Sweet #{@name}, let us get to the next one"
      person_return = [@name, @age,@origin]
      return person_return
    else 
      puts "Get off. Let us try again"  # How to go retry?
    end
  end
end

person_one = Person.new
var_person_one = person_one.input(1)
puts "Press Enter for next Person\n"
gets

person_two = Person.new.input(2)
var_person_two = person_two
puts "Press Enter for next Person\n"
gets

person_three = Person.new.input(3)
var_person_three = person_three

p var_person_one
p var_person_two
p var_person_three

Output full code snippet:

> Hi, let's create three new Person. Start with number 1
> What's your name?
> Kevin
> How old are you?
> 29
> Where are you from?
> Hamburg
> I summarize! You are Kevin and 29 years old from Hamburg
> Is that correct? 
> Enter "0=Yeah" or "Nah"
> 0
> Sweet Kevin, let us get to the next one
> Press Enter for next Person
> 
> Hi, let's create three new Person. Start with number 2
> What's your name?
> Steven
> How old are you?
> 22
> Where are you from?
> London
> I summarize! You are Steven and 22 years old from London
> Is that correct? 
> Enter "0=Yeah" or "Nah"
> 0
> Sweet Steven, let us get to the next one
> Press Enter for next Person
> 
> Hi, let's create three new Person. Start with number 3
> What's your name?
> Mike
> How old are you?
> 33
> Where are you from?
> Tampa,FL
> I summarize! You are Mike and 33 years old from Tampa,FL
> Is that correct? 
> Enter "0=Yeah" or "Nah"
> 1
> Get off. Let us try again
> ["Kevin", "29", "Hamburg"]
> ["Steven", "22", "London"]
> nil

Thank you very much in advance!!!

You really should be constructing the Person with the initialize method. You should start with a proper Person class like this:

class Person                                                                   
                                                                               
  attr_accessor :name, :age, :origin                                           
                                                                               
  def initialize(name, age, origin)                                            
                                                                               
    @name = name                                                               
    @age = age                                                                 
    @origin = origin                                                           
                                                                               
  end                                                                          
                                                                               
end

Then separate from the Person class you should have a method that can prompt the user for info and create a Person object… Something like below. Note the code is not 100%

def get_person

  print "Enter name: "
  name = gets.chomp

  print "Enter age: "
  age = gets.chomp

  print "Enter origin: "
  origin = gets.chomp

  print "Is this correct(y/n): name = #{name}, age = #{age}, origin = #{origin}?"
  ans = gets.chomp

  if ans == 'y'
    Person.new(name, age, origin)
  else
    get_person
  end
  
end

From what I can see, you are suffering from a common problem… Trying to do too much in one method.

If you are just starting out then think of classes as repositories for data. The initialize method should take in values and initialize the object’s data. The initialize method shouldn’t be prompting the users or opening sockets or files. The initialize method has one function, to initialize the objects data. If you have to get that data from somewhere then do it outside of the initialize method. The rest of the class’s methods should provide basic access to the object’s data. You should be able to read the data and you should be able to update the data with new values.

I know this is a over simplification but its a good starting point and it should eliminate the one method does everything approach.

1 Like