0

I have a array of hashes

pets = [
    {
    "firstName": "Brianna",
     "lastName": "Parson",
     "children": ["sam","Joe"],
     "buddys": {
     
    },
     
    {
     "firstName": "jeffery",
     "lastName": "thomas",
     
     }
     }
    ] 

How do you send a class an array of hashes?

mechnicov
  • 12,025
  • 4
  • 33
  • 56
  • Why not just iterate over the array of hashes and output the information the way you want? Why both creating `User` objects for each hash? – Chris Feb 08 '22 at 21:09

3 Answers3

2

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

theplaceofburak
  • 160
  • 1
  • 13
1

You can do it like this

users = [
  {
    "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"
    }
  }
] 
class User
  def initialize(args)
    @first_name = args[:firstName].capitalize
    @last_name = args[:lastName].capitalize
    @job = args[:job].downcase
    @total_asset_worth = args[:totalAssetWorth].to_f
    @total_debt = args[:totalDebt]
    @children = args[:children]
    @pets = args[:pets]
  end

  def to_s
    "#{@first_name} is a #{@job} and has #{@children.count} children and #{@pets.count} pet"
  end
end
puts users.map { |args| User.new(args) }

# Brianna is a software engineer and has 2 children and 2 pet
# Jeffery is a lawyer and has 0 children and 1 pet

Some Ruby magic: when pass array to puts method, each element will be put on new line and to_s method will be applied

mechnicov
  • 12,025
  • 4
  • 33
  • 56
0

Here is a simple class.

class Person
  def initialize(full_name, job, num_children, num_pets)
    @full_name = full_name
    @job = job
    @num_children = num_children
    @num_pets = num_pets
  end
  
  def to_s
    format(
      '%s is a %s and has %d children and %d pets',
      @full_name,
      @job,
      @num_children,
      @num_pets
    )
  end
end

This is just an example. Your final class design will depend on your application.

See http://ruby-doc.com/docs/ProgrammingRuby/ for a more complete introduction to classes.

Jared Beck
  • 16,796
  • 9
  • 72
  • 97