如何将新项目添加到散列 [英] How to add new item to hash

查看:77
本文介绍了如何将新项目添加到散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby的新手,不知道如何将新项目添加到已存在的散列。例如,我首先构造了散列:

I'm new to Ruby and don't know how to add new item to already existing hash. For example, first I construct hash:

hash = {:item1 => 1}

之后,想要添加item2,所以之后我有这样的散列:

after that a want to add item2 so after this I have hash like this:

{:item1 => 1, :item2 =>2}

我不知道在hash上做什么方法,有人可以帮我吗?

I don't know what method to do on hash, could someone help me?

推荐答案

创建哈希:

Create the hash:

hash = {:item1 => 1}

添加一个新项目:

hash[:item2] = 2

这篇关于如何将新项目添加到散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆