I am developing a hospital management system on command line. I want to take a value from a user, assign it to an instance variable, and further store it in an array. The code is as follows:
def doctor_details
@doctors = Array.new
puts 'Enter Doctor Name'
@doc_name = gets
@doctors << @doc_name
puts 'Enter specialization'
@doc_specialization = gets
puts 'Availability of doctor'
@from = Float(gets)
@to = Float(gets)
end
Every time a new value is entered, it overwrites the previous value.
Whatever you have written it will append the input to the instance variable @doctors for that particular run i.e for that particular @doctor. If you need want to store the details of all the doctors in one instance variable then declare it outsite of the method and then run it like below. It will be better if you store the doctor details as array of arrays like DOCTORS = [[DOCTOR1_DETAILS],[DOCTOR1_DETAILS]] you can do this by
@DOCTORS = []
def doctor_details
@doctor =[]
puts 'Enter Doctor Name'
doc_name = gets
@doctor << doc_name
puts 'Enter specialization'
doc_specialization = gets
@doctor << doc_specilalization
puts 'Availability of doctor'
from = Float(gets)
to = Float(gets)
@doctor << from
@doctor << to
@doctors << @doctor
end
OR You can simply append the whole details to an array using .push method like this
@doctors = []
def doctor_details
puts 'Enter Doctor Name'
doc_name = gets
puts 'Enter specialization'
doc_specialization = gets
puts 'Availability of doctor'
from = Float(gets)
to = Float(gets)
@doctors.push([doc_name,doc_specialization,from,to])
end