我對Ruby只有極少的知識,但我正在爲我的辦公室開發一個Vagrant VM。我將變量中的設置配置爲允許我們的每個開發人員輕鬆進行自定義,但是當我嘗試從外部文件包含變量時遇到了問題。奇怪的Ruby範圍界定問題(我認爲)
下面是我在做什麼(這工作)的基本要點是:
# Local (host) system info
host_os = "Windows"
nfs_enabled = false
# IP and Port Configuration
vm_ip_address = "33.33.33.10"
vm_http_port = 80
host_http_port = 8888
vm_mysql_port = 3306
host_mysql_port = 3306 # Warning, mysql port configuration using 3306 will interfere with any locally run MySQL server.
local_sites_path = "D:\\Web"
vm_sites_path = ENV["HOME"] + "/Sites"
# VM Configuration
vm_memory = "1024"
Vagrant.configure("2") do |config|
... do vagrant stuff here
然而,這不起作用(config.local.rb的內容符合上述變量聲明):
if(File.file?("config.local.rb"))
require_relative 'config.local.rb'
else
# Local (host) system info
host_os = "Mac"
nfs_enabled = true
# IP and Port Configuration
vm_ip_address = "33.33.33.10"
vm_http_port = 80
host_http_port = 8888
vm_mysql_port = 3306
host_mysql_port = 3306 # Warning, mysql port configuration using 3306 will interfere with any locally run MySQL server.
local_sites_path = ENV["HOME"] + "/Sites"
vm_sites_path = ENV["HOME"] + "/Sites"
# VM Configuration
vm_memory = "512"
end
Vagrant.configure("2") do |config|
... do vagrant stuff here
任何想法這裏發生了什麼?在這兩種情況下,變量聲明都在文件的頂部,所以我的理解是它們應該在全局範圍內。
這裏是config.local.rb的內容:
# Duplicate to config.local.rb to activate. Override the variables set in the Vagrantfile to tweak your local VM.
# Local (host) system info
host_os = "Windows"
nfs_enabled = false
# IP and Port Configuration
vm_ip_address = "33.33.33.10"
vm_http_port = 80
host_http_port = 8888
vm_mysql_port = 3306
host_mysql_port = 3306 # Warning, mysql port configuration using 3306 will interfere with any locally run MySQL server.
local_sites_path = "D:\\Web"
vm_sites_path = ENV["HOME"] + "/Sites"
# VM Configuration
vm_memory = "1024"
正如我所說的,我還沒有真正使用過的Ruby,但我知道編程和範圍都表示,這應該是工作的罰款。我已經檢查過(使用print
語句)該腳本正在檢測幷包含該文件,但由於某種原因,除非在Vagrantfile中直接對配置設置進行硬編碼,否則它不起作用。
在此先感謝。
非常延遲的響應,但是這是問題。我沒有意識到ruby的全局變量需要'$'。 – ChimericDream