Reading variables from a file in Ruby

Is there a way to read environment variables in a file?

In bash, I have an env.sh file that I can use

env.sh

foo="bar" 

bash file

 set -a source env.sh 

This would allow me to simply use foo, as if I had split it into a ruby ​​script.

Also, is there a way to make sure this file is unreadable so that passwords can be stored in this file?

+8
source share
3 answers

It looks like you should provide a sample file for the user / administrator to modify for your personal environment, and then populate the environment from this, avoiding possibly having this file with sensitive information in the repository. Note. Regarding file security, you need to decide where the file is located, as well as your operating system and server software.

If so, you can provide a file containing a template of what you need from the administrator / user of the program you are setting up.

Ruby has an ENV constant that acts like a Hash and contains the shell environment used.

As an example, you can find a file called environment.rb.sample that will be shared with someone. It contains instructions and contains a template that users can freely change, with instructions for copying the file to environment.rb . An example file is as follows:

 # environment.rb.sample # Copy this file to environment.rb and change the name and password to your credentials ENV['temp_user_name'] = 'Foo Bar' ENV['temp_password'] = 'Dazz Kezz 

Then the file is copied to this file, perhaps:

 # environment.rb ENV['temp_user_name'] = 'Joe Admin' ENV['temp_password'] = 'Super Secure Password' 

The file that downloads it and uses it is just a Ruby file that is freely modified by the user / administrator of the software and looks like this, as well as publicly available.

 # load_environment require './environment' puts ENV['temp_user_name'] puts ENV['temp_password'] 

Loads the file and uses ENV , which is a constant limited to global areas for the application.

The file permissions are then controlled by the user / system administrator and protected like any other confidential information in their system. A sensitive file must also be specified in the repository ignore mechanism. It should never be published.

+5
source

Yes, there is, and if for some bizarre, incomprehensible reason you should use it, it matters:

A WARNING. Never use this for a user-provided file.

And, if you do not have a very, very specific need, do not use it in working code.

 eval(File.read("name_of_var_file"), binding) 

If you are really trying to write a configuration file, use YAML. A file like this:

config.yaml:

 foo: "bar" 

You can access it as follows:

 require 'yaml' conf = YAML.loads(File.read("config.yaml")) conf['foo'] #=> 'bar' 

It is safe and manageable, not to mention standard practice.


As for the inaccessibility of the file, this is a problem of the operating system level, which cannot be solved without information about the environment, OS, settings, etc.

+2
source

The purpose of a local variable should be used temporarily in a method or block definition. Using it outside of such environments, especially in files, strikes its purpose. You do not need to do this, and Ruby has no easy way to do this.

If you use variables correctly and want to share variables between files, these must be other types of variables, such as instances, classes, or global variables. Or, in order to set up your environment, you must use constants. Among them, global variables and constants can be written to a file, loaded into another file and will be used.

file a.rb

 $foo = 1 FOO = 2 

b.rb file

 load "file-a.rb" $foo # => 1 FOO # => 2 

As for instances and class variables, they belong to the class or instance, so they must be defined in such an environment. And you can re-open the same class in another file and load it into another file.

file a.rb

 class Bar @@foo = 1 def initialize; @foo = 2 end end 

b.rb file

 load "file-a.rb" Bar.class_variable_get("@@foo") # => 1 Bar.new.instance_variable_get("@foo") # => 2 
+2
source

Source: https://habr.com/ru/post/950943/


All Articles