简体   繁体   English

如何将对象存储在Ruby中的哈希中?

[英]how store object in hash in ruby?

I have a hash to objects MyObject in a hash: 我在哈希中有一个对象MyObject的哈希:

inst = Hash.new(0)
inst = {"val1" => MyObject.new(0x18,3,4)}

But when I want to get the element: 但是当我想要获取元素时:

puts inst["val1"]

I get an error: 我收到一个错误:

MyObject:0x00000002468690

Hash are Key-value pair . 哈希是键值对。 in this case what you are getting is value for the given key ( which is "val1"),which is instance of MyObject class. 在这种情况下,您得到的是给定键(即“ val1”)的值,该键是MyObject类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM