Reputation: 55
I am working on a database that holds records for a school, where the key is the studentID, followed by the values of, first name, last name, major, and catalog year. I am working on the display function, which loops through the users which have been added to the hash. however, my code is not printing out of all the records i have added to the database.
it is only printing out one record listing, rather than my multiple inputted entries.
here is an example of input: ----------------------------- Student Database Records -----------------------------
1) Insert new record to database
2) Modify record in database
3) Remove record from database
4) Display record(s) in database
5) Quit
6) Enter choice:
1
-----------------------------
Add Record(s)
-----------------------------
Enter Student Identifcation Number:
32424
Enter First name of Student:
sfsdf
Enter Last name of Student:
sdfsfsf
Enter Major of Student:
sdfsdfs
Enter Catalogue Year:
sdfsfds
Your entry for Student ID 32424 has been added to the database.
------------------------------------------------------------------------
32424: sfsdf, sdfsfsf, sdfsdfs, sdfsfds
------------------------------------------------------------------------
here is my code to add an array to a hash
student_id = gets().chomp
if school_database.sDB.has_key?(student_id)
puts "Student Record Already Existent"
return school_database
end
puts "\nEnter First name of Student: "
first_name = gets().chomp
puts "\nEnter Last name of Student: "
last_name = gets().chomp
puts "\nEnter Major of Student: "
major = gets().chomp
puts "\nEnter Catalogue Year: "
catalogue_year = gets().chomp
puts "\nYour entry for Student ID #{student_id} has been added to the database.\n"
puts "\n------------------------------------------------------------------------"
puts "#{student_id}: #{first_name}, #{last_name}, #{major}, #{catalogue_year}"
puts "------------------------------------------------------------------------\n\n"
store_account_data = first_name + "," + last_name + "," + major + "," + catalogue_year
school_database.sDB[student_id] = [store_account_data]
return school_database
here is the code i am using to loop through my hash to print out the records.
school_database.sDB.each do |key, store_account_data|
puts "\n"
puts "#{key}: #{store_account_data.join(',')}"
positively, I run the .size command, and i discovered that it is adding muliple entries to the hash, however, it is not printing all of them
any ideas?
Upvotes: 0
Views: 367
Reputation: 9314
String’s +
method places fairly strict requirements on what can appear on the right-hand side. You need to explicitly call to_s
:
puts key + ' : ' + store_account_data.to_s
String interpolation is much more forgiving; it basically calls to_s
for you:
puts "#{key} : #{store_account_data}"
Or perhaps you want a more detailed dump:
puts "#{key} : #{store_account_data.inspect}"
Or no brackets:
puts "#{key} : #{store_account_data.join(', ')}"
Or perhaps elements of store_account_data
are objects, and you want to just print one property of them:
puts "#{key} : #{store_account_data.map(&:field_to_print).join(', '}"
Upvotes: 2
Reputation: 106812
I would do something like:
def display(database)
database.sDB.each do |key, account|
puts "#{key}: #{account.join(',')}"
end
end
Upvotes: 1