ruby 将散列数组发送给类以从每个散列创建一个用户

pxyaymoc  于 11个月前  发布在  Ruby
关注(0)|答案(3)|浏览(118)

我有一个哈希数组

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

如何给一个类发送一个哈希数组?

2admgd59

2admgd591#

你不需要任何初始化方法的东西。我把散列传递给了初始化方法

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

当你用一个新方法创建一个类的示例时,首先,初始化方法将执行,然后,你应该调用show_userhash方法。
这里是完整的代码。

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
zhte4eai

zhte4eai2#

你可以这样做

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

Ruby魔法:当传递数组到puts方法时,每个元素将被放在新的一行上,并应用to_s方法

u4vypkhs

u4vypkhs3#

这是一个简单的类。

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

这仅仅是一个例子。最终的类设计将取决于您的应用程序。
请参阅http://ruby-doc.com/docs/ProgrammingRuby/以获得对类的更完整的介绍。

相关问题