簡體   English   中英

是否可以將 ruby​​ 哈希從一個文件讀取到另一個文件中,然后使用其值?

[英]Is it possible to read a ruby hash from one file into another and then use its values?

背景

我有一個項目結構,其中我有一個 .rb 文件,其中包含散列中的數據,這不是文件中唯一的內容:

name "vm"
description "Configuration file for the Demo VM"
default_attributes(
    custom_demo: {
        verticals: {
            fashion: true,
            automotive: false,
            fsi: false,
            custom: true
        },
        channels: {
            b2b: true,
            b2c: true
        },
        geos: [
            'us_en'
        ]
    },
    infrastructure: {
        php: {
            version: '7.3',
            port: 9000
        },
        webserver: {
            http_port: 80,
            ssl_port: 443
        },
        database: {
            user: 'magento',
            password: 'password',
            name: 'magento'
        },
        elasticsearch: {
            use: true,
            version: '6.x',
            memory: '1g',
            port: 9200,
            plugins: ['analysis-phonetic', 'analysis-icu']
        },
        mailhog: {
            use: true,
            port: 10000
        },
        webmin: {
            use: true,
            port: 20000
        },
        samba: {
            use: true,
            shares: {
                composer_credentials: true,
                image_drop: true,
                web_root: true,
                app_modules: true,
                multisite_configuration: true,
                app_design: true
            }
        }
    }

在另一個 ruby​​ 腳本中,我需要使用這個default_attributes散列中的值來做其他事情。

我的問題

在另一個 ruby​​ 腳本中使用上述 ruby​​ 哈希的最佳方法是什么?

我試過的

首先,我嘗試使用load()來“加載”帶有哈希值的文件。 由於前兩行,這引發了一個問題:

name "vm"
description "Configuration file for the Demo VM"

所以,我想我會把它當作一個字符串或數組來讀,並跳過前兩行:

data_string = ''
data = File.readlines(File.dirname(File.expand_path(__FILE__)) + '/environments/vm.rb').drop(2).each do |line| 
  data_string += line
end
data_hash = JSON.parse(data_string)
print data_hash

這種方法有兩件事我不喜歡:首先,它將結果打印到屏幕上,其次,它會出錯:

/opt/vagrant/embedded/lib/ruby/2.4.0/json/common.rb:156:in `parse': 751: unexpected token at 'verticals: { (JSON::ParserError)

正是在這一點上,我開始懷疑我的方法,並懷疑我想做的事情是否可能。 為了澄清,理想情況下,在弄清楚如何在另一個腳本中解析上述文件后,我將能夠使用哈希中的某些內容,例如:

default_attributes[:infrastructure][:php][:version]
# => 7.3

嘗試直接從該文件讀取散列是可行的,但很麻煩 我認為你應該從不同的角度來看待它。 與其直接從這個文件中讀取哈希,不如將哈希移動到它自己的文件中,然后從兩個地方讀取它。 所以:

  1. 將哈希放在它自己的文件中:

     # my_hash.rb MyHash = { # put the hash content here }
  2. 從 vm 配置文件加載它:

     # vm_config.rb require_relative './my_hash.rb' name "vm" description "Configuration file for the Demo VM" default_attributes(MyHash)
  3. 也可以從任何其他文件加載它:

     # other_file.rb require_relative './my_hash.rb' puts MyHash # => hash will print, it has been loaded

就像練習一樣(不要這樣做!),這是我直接從文件中讀取哈希的方法:

# other_file.rb

# dummy methods:
def name(*args); end;
def description(*args); end

# intercept the hash and assign it to a global
def default_attributes(hash)
  $my_hash = hash
end

# require the vm config file, running the above functions:
require_relative './vm_config.rb'

puts $my_hash
# => prints the hash

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM