我想知道是否可以將變數值或字串連接到 Ruby 中的新變數值宣告。
foo = "something"
#new variable declaration:
var_ foo = "concat variable name"
p var_foo # => "concat variable name"
n = 2
position n = Array.new(3, 1)
p position2 # => [1, 1, 1]`
非常感謝
uj5u.com熱心網友回復:
在這種情況下,使用Hash可能會更好。
values = {}
values['foo'] = 'something'
values['var_' 'foo'] = 'concat variable name'
p values['var_foo'] #=> "concat variable name"
n = 2
values["position#{n}"] = Array.new(3, 1)
p values['position2'] #=> [1, 1, 1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/520429.html
