ruby hash value array append new element

ruby 中的hash 初始化value 为array 

mh = Hash.new(Array.new)
mh[3]  << 4
irb(main):034:0> mh[3]
=> [4]
irb(main):035:0> mh
=> {}

无法直接采用上述方式追加元素;

可采用如下形式,

ht1 = Hash.new {|h,k| h[k]=[]} 


ruby float format

"Current amount: #{amount.round(2)}"
 puts "Current amount: #{format("%.2f", amount)}"
 
原文地址:https://www.cnblogs.com/lavin/p/11022943.html