skip to Main Content

I am supplying environment variables in docker run as docker run --env-file <env_file_path>..... The Dockerfile executes a ruby script which reads the env variables.

# environment variable file
TEST_1=NULL
TEST_2=
TEST_3=""
# ruby script
print ENV['TEST_1'] # "NULL"
print ENV['TEST_2'] # ""
print ENV['TEST_3'] # """"

How can I receive a nil values in ruby from the environment variables? What should the supplied environment variable value be?

3

Answers


  1. Does it have to be nil? Can you use undefined instead?

    If you’re doing an if check in the code: if env.test_1 then you could just not define it in the file.

    Login or Signup to reply.
  2. How can I receive a nil values in ruby from the environment variables? What should the supplied value be?

    You can’t, not directly. Unlike a language like ruby, environment variables do not have a concept of null values.

    You’ll have to check for and convert a chosen value like an empty string, NULL or nil to an actual nil in your code.

    You could use the mere presence / absence of the env variable, but the issue with that is that you can’t unset an environment variable once it’s set with docker, only set it to empty string.

    Login or Signup to reply.
  3. The value of an environment variable (in terms of the operating system) is a string, and you can’t beat this, in particular not with a language which has a very unusualy idea of a null pointer (since nil is an object of a full-featured class in Ruby, much different to i.e. C or Java).

    Usually (when seen from i.e. shell programming), we sometimes distinguish between an environment variable which is empty, and one which is undefined. This you can do in your Ruby too:

    ENV.has_key?('V') # -> true if defined, false if undefined
    ENV['V'] # -> trueish if defined, nil if undefined
    

    If you know that an environment variable is defined, you can check whether it is empty, by

    ENV['V'].empty?
    

    Transfering the Ruby type system to a data model established by the whole operating system, is usually futile, however there is a way, if you can live with restrictions:

    Assuming that you need the environment variables to transport Ruby data (nil, arrays of strings, or whatever) from one Ruby process to another Ruby process, you can of course serialize the data. Serialization means that you create a string representation of the data (which makes it suitable for being stored in an environment variable) and deserialize it in the receiveing process.

    Ruby comes with 3 serialization methods: JSON, YAML and (the most general and flexible one) Marshal. With JSON and YAML, you have even a chance to deserialize in a different language. With Marshal, you have to use the same Ruby implementation on the sending and receiving end.

    With all serialization methods, you indeed can represent a Ruby nil in an Environment variable, for instance:

    ENV['V']=Marshal.dump(nil) # serializing
    
    v=Marshal.load(ENV['V']) # deserializing
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search