如何将多个值分配给散列键? [英] How can I assign multiple values to a hash key?

查看:90
本文介绍了如何将多个值分配给散列键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了方便起见,我试图在Ruby中为多个值分配一个散列键。这里是目前的代码

  myhash = {:name => [Tom,Dick,Harry]} 

输出:

 名称:TomDickHarry $ $ 3 $值的字符串

b $ b

所需输出:

 :name => Tom,:name => Dick,:name => Harry

必须写出什么代码才能获得所需的输出?$ b $您已经创建了一个符号名称作为键和一个包含三个元素作为值的数组的散列,因此您需要遍历 myhash [:name] 来获取单个数组元素。


For the sake of convenience I am trying to assign multiple values to a hash key in Ruby. Here's the code so far

myhash = { :name => ["Tom" , "Dick" , "Harry"] }

Looping through the hash gives a concatenated string of the 3 values

Output:

name : TomDickHarry

Required Output:

:name => "Tom" , :name => "Dick" , :name => "Harry"

What code must I write to get the required output?

解决方案

You've created a hash with the symbol name as the key and an array with three elements as the value, so you'll need to iterate through myhash[:name] to get the individual array elements.

这篇关于如何将多个值分配给散列键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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