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.
嗨,我想将这些值放入哈希中并检索它们。
c={} a={"cat"=> 1,"cap"=> 2} b={"rat"=> 12,"soap"=> 5}
现在如何将值“a”和“b”放入值“c”?
并从值 c 中检索这些值 a 和 b?
谢谢。
你不能推入哈希。
添加到哈希中的每个值都必须有一个键。所以在你的情况下,你可以做
c[key1] = a c[key2] = b
你的哈希现在变成c = {"key1" => {"cat" => 1,"cap" => 2}, "key2" => {"rat" => 12,"soap" => 5}}
c = {"key1" => {"cat" => 1,"cap" => 2}, "key2" => {"rat" => 12,"soap" => 5}}
您可以通过 检索它们c[your_key]。
c[your_key]