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!!!