Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 22, 2019 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/785a954acf44e5f3ba176859660316e7 to your computer and use it in GitHub Desktop.
Save parzibyte/785a954acf44e5f3ba176859660316e7 to your computer and use it in GitHub Desktop.
=begin
Leer variables del entorno de un archivo
.env (cuyo nombre no es obligatoriamente ese)
usando Ruby y la gema dotenv
@author parzibyte
=end
# Cargamos la gema
require 'dotenv'
# Y cargamos el archivo que tiene las variables
# También es válido Dotenv.load('.env', 'otro_archivo.txt', ...)
Dotenv.load('.env')
# Nos aseguramos de que las claves estén presentes, así
# no tenemos errores más tarde en tiempo de ejecución; lo
# detenemos aquí
#Nota: no soportado todavía (31/12/2018 pero en el repositorio dicen que lo agregarán)
#Dotenv.require_keys("USUARIO_MYSQL", "CLAVE_RSA_PUBLICA")
# Hora de leer una variable
usuario_mysql = ENV['USUARIO_MYSQL']
puts "El usuario de MySQL es: '#{usuario_mysql}'"
host_mysql = ENV['HOST_MYSQL']
puts "El host de MySQL es: '#{host_mysql}'"
clave_rsa = ENV['CLAVE_RSA_PUBLICA']
puts "La clave RSA: '#{clave_rsa}'"
pi = ENV['PI'].to_f
puts "pi * pi = #{pi * pi}"
# Si no existe, devuelve nil
variable_inexistente = ENV['INEXISTENTE']
if variable_inexistente === nil
puts "Al leer, ¡es nil!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment