0
我試圖在Ruby中調用RRD.create
。我的RRD變量存儲在散列表中,我需要構造一個RRD.create
調用。這裏是我的代碼:如何在Ruby中調用函數時將數組轉換爲字符串列表?
pool_map = {
"cleanup" => "Cleaning up",
"leased" => "Leased",
"ready" => "Ready"
}
start = Time.now.to_i
ti = 60 # time interval, in seconds
RRD.create(
rrdfile,
"--start", "#{start - 1}",
"--step", ti, # seconds
pool_map.keys.map{|val| "DS:#{val}:GAUGE:#{ti * 2}:0:U" }.collect,
"RRA:LAST:0.5:1:#{86400/ti}", # detailed values for last 24 hours
"RRA:AVERAGE:0.5:#{5*60/ti}:#{7*24*60}", # 5 min averages for 7 days
"RRA:MAX:0.5:#{5*60/ti}:#{7*24*60}", # 5 min maximums for 7 days
"RRA:AVERAGE:0.5:#{60*60/ti}:#{183*24}", # 1 hour averages for a half of the year
"RRA:MAX:0.5:#{60*60/ti}:#{183*24}" # 1 hour maximums for a half of the year
)
不過,我從紅寶石得到以下錯誤:
in `create': invalid argument - Array, expected T_STRING or T_FIXNUM on index 5 (TypeError)
我需要指定若干字符串RRD.create
強似陣列。我如何在Ruby中做到這一點?
P.S. http://oss.oetiker.ch/rrdtool/prog/rrdruby.en.html
我從未想過使用啪的一聲! (我只是真的在方法定義中使用它,就像在「數組中未知數的參數」中一樣)。這真棒,我更喜歡Ruby,我之前做過 –
非常感謝splat操作員的建議!我應用了它,但仍然存在問題: –
我已將行更改爲'* pool_map.keys.map {| val | 「DS:#{val}:GAUGE:#{ti * 2}:0:U」},但仍然會出現很多錯誤,例如'語法錯誤,意外的tSTRING_BEG,期待tAMPER' –