我有一个哈希数组
pets = [
{
"firstName": "Brianna",
"lastName": "Parson",
"children": ["sam","Joe"],
"buddys": {
},
{
"firstName": "jeffery",
"lastName": "thomas",
}
}
]
如何给一个类发送一个哈希数组?
3条答案
按热度按时间2admgd591#
你不需要任何初始化方法的东西。我把散列传递给了初始化方法
正如你意识到的,用户信息在一个数组中,所以首先,你应该遍历数组,你应该用哈希语法获取所有哈希信息。
当你用一个新方法创建一个类的示例时,首先,初始化方法将执行,然后,你应该调用
show_userhash
方法。这里是完整的代码。
zhte4eai2#
你可以这样做
Ruby魔法:当传递数组到
puts
方法时,每个元素将被放在新的一行上,并应用to_s
方法u4vypkhs3#
这是一个简单的类。
这仅仅是一个例子。最终的类设计将取决于您的应用程序。
请参阅http://ruby-doc.com/docs/ProgrammingRuby/以获得对类的更完整的介绍。