Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
键必须是字符串或整数,还是可以是任何对象类型?
一个非常常见的类型是 Symbol,你没有提到。但它可以是任何对象,真的。
class Foo; end f1, f2 = Foo.new, Foo.new h = { f1 => 3, f2 => 4 } h # => {#<Foo:0x007fed4b04bb00>=>3, #<Foo:0x007fed4b04bad8>=>4} h[f1] # => 3 h[f2] # => 4
没有记录?
[Hash] 类似于 Array,除了索引是通过任何对象类型的任意键完成的,而不是整数索引。
(强调我的。)