You don't need anything that initialize method.I passed the hash the initialize method
def initialize(user)
@users = user
end
And also As you realized that, the users information in an array, so firstly, you should loop through the array and you should take all hash information with hash syntax.
def show_userhash
@users.each do |user|
puts "#{user[:firstName]} is a #{user[:job]} also has #{user[:pets].length} #{pet_num = user[:pets].length == 1 ? "pet" : "pets"} "
end
end
When you made an instance of a class with a new method, first, the initialize method will execute and then, you should invoke the show_userhash
method.
Here is the full code.
class Person
def initialize(user)
@users = user
end
def show_userhash
@users.each do |user|
puts "#{user[:firstName]} is a #{user[:job]} also has #{user[:pets].length} #{pet_num = user[:pets].length == 1 ? "pet" : "pets"} "
end
end
end
person_information = [
{
"firstName": "Brianna",
"lastName": "Parson",
"job": "SOFTWARE ENGINEER",
"totalAssetWorth": "2000.00",
"totalDebt": "1500.00",
"children": ["sam", "Joe"],
"pets": {
"dog": "jeff",
"bird": "zoe"
}
},
{
"firstName": "Jeffery",
"lastName": "thomas",
"job": "Lawyer",
"totalAssetWorth": "6000.00",
"totalDebt": "1300.00",
"children": [],
"pets": {
"cat": "gnocchi"
}
}
]
intro = Person.new(person_information)
intro.show_userhash