我想從#{vm}中獲得第5到第7個字符,但是我似乎無法使其工作。我錯過了什麼?紅寶石 - 從字符串獲得第n個字符
puts "#{Colorable.wrap_color("Restarting VM's", :BLUE)}"
change_set.vms_to_restart.each do |vm|
puts "JH 0 - " + %x{hostname}
host_code = %x{hostname}[7,9]
vm_code = #vm[5,7] <---- THIS DOES NOT WORK
puts "JH 1 - #{host_code}"
puts "JH 2 - #{vm_code}"
puts "JH 3 - #{vm}"
puts "Restarting ... #{vm}"
exit 1
vm.restart
end
puts " "
輸出:
Restarting VM's
JH 0 - qdscild401
JH 1 - 401
JH 2 - <---------- BLANK
JH 3 - qdcld401.vm10
Restarting ... qdcld401.vm10
怎麼樣'vm_code = 「#{VM [5,7]}」' –
不能再現。 – sawa
上面的vm_code =「#{vm [5,7]}」給了我這個錯誤: ./do_changes.rb:76:未定義的方法'[]'爲#(NoMethodError) from ./do_changes.rb:72:in'each' from ./do_changes.rb:72 –
Jaron787