Ruby Arrays & Hashes
Today we learnt about hashes & arrays & playing with them! It certainly helped to have prior knowledge of arrays in JavaScript before I started!
I wrote this code this afternoon as practice for hashes & arrays. In this example I’ve used Arrays within the main person hash:
person = {details: [{ name: "Katie", age: 32 },{ name: "John", age: 21}],jobs: [{ job: "Programmer", salary: 100000000 },{ job: "Office Worker", salary: 20000 }] }puts "#{person[:details][0][:name]}'s job is: #{person[:jobs][0][:job]}, her age is #{person[:details][0][:age]} & her salary is: #{person[:jobs][0][:salary].to_s}"puts "#{person[:details][1][:name]}'s job is: #{person[:jobs][1][:job]}, his age is #{person[:details][1][:age]} & his salary is #{person[:jobs][1][:salary].to_s}"# Outputs Person 1 as:
# Katie's job is: Programmer, her age is 32 & her salary is: 100000
What I’ve done here is create a hash named person, inside that hash are two arrays; details & jobs.
Inside those arrays are hashes which provide key pairs with different values. I am accessing the different values by using array indexing & symbols.
I’m pretty proud of myself & tomorrow I’m going to make it more interactive with user input!