0

私は Ruby について最小限の知識しか持っていませんが、オフィス用に Vagrant 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"

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 に直接ハードコーディングしない限り機能しません。

前もって感謝します。

4

2 に答える 2

1

小文字で始まる変数はローカル変数です。それらは、定義されているスコープに対してローカルであるため、「ローカル」変数と呼ばれます。あなたの場合、それらは のスクリプト本体に対してローカルですconfig.local.rb。のスクリプト本体以外からはアクセスできませんconfig.local.rb。それが彼らを「ローカル」にするものです。

グローバル変数が必要な場合は、グローバル変数を使用する必要があります。グローバル変数は$記号で始まります。

于 2013-05-30T14:45:54.723 に答える