将新对添加到现有哈希

时间:2014-04-22 14:41:05

标签: ruby

我需要以某种方式增加价值: sizes = { :thumb => ["100x100"] } 现有的哈希。

问题是,:thumb的含义是一个变量,所以制作

sizes[@thumb] = ["100x100"] 
# or 
sizes.merge!(@thumb=>["100x100"]) 

# the result is:
> sizes = { "thumb" => ["100x100"] }

我的应用"thumb"不被接受。它需要提供:thumb密钥格式。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

怎么样:

sizes[@thumb.to_sym] = ["100x100"] 
# or
sizes.merge!(@thumb.to_sym => ["100x100"])