Read environment variables with ruby ​​file [Super beginner]

I decided to touch ruby ​​for the first time in a few years, so I tried using Google's SpeechToText API. In the process, I had a hard time forgetting how to read the environment variables described in the .env file in the .rb file, so I will summarize them.

First, create an .env file

Place the .env file in the same directory as the .rb file

Dotenv installation

Read environment variables using a gem called dotenv What kind of gem is What is dotenv? .

Do the following on the command line

$ gem install dotenv

And

bundle install

Write an environment variable reading method in the .rb file

sample.rb


require 'dotenv'
Dotenv.load

that's all

Recommended Posts

Read environment variables with ruby ​​file [Super beginner]
[Super easy] Ruby environment construction
Read dump file with Docker MySQL
[Ruby] Handle instance variables with instance methods
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Handle application/pdf response as File with ruby
Read xlsx file in Java with Selenium
Read a string in a PDF file with Java
AtCoder Beginner Contest 169 A, B, C with ruby
Variables / scope (ruby)
About Ruby variables
[Spring] Read a message from a YAML file with MessageSource
Range where variables can be used with ruby [Scope]
Build ruby debug environment with VS Code of Windows 10