I am trying to create a simple bank account program to practice some Ruby. The idea is to create an array of hashes. However, I want to use user input to create a new hash, whether that be creating the title or simply numbering the entries something like user1 or user2.
If I use a set up that stores the username, pin, and balance all inside the hash, like so:
jhip = {
:pin => 1234,
:balance => 1000000
}
jdog = {
:pin => 2345,
:balance => 1000
}
accounts = [
jhip,
jdog
]
I feel it will be much easier to find the right username in the array and/or tell the user that their name has already been chosen than if I used a set up like this:
user1 = {
:username => "jhip",
:pin => 1234,
:balance => 1000000
}
user2 = {
:username => "jdog",
:pin => 2345,
:balance => 1000
}
accounts = [
user1,
user2
]
The real meat of this problem is getting the user input to be the title of the hash. I know the notation for creating a hash like this:
h = hash.new #or
hash = {}
But I don't know how to use the input as a title. When I store the input as a variable, I can play with the variable:
@username = gets.chomp
@username.to_i #or any method
But if I try to create a hash with the variable name, I overwrite the input.
@username = {} #no more using the response from gets
I guess what I'm looking for would be something like
@username = gets.chomp
@username.to_s => (some sort of code or command I'm unaware of) {}
And then I can push the key and value pairs into the hash. I don't know if I could use a block to accomplish this or something else. I'm still new to this, so I apologize if I bit off more than I can chew with this problem. Thanks for any help.