DEV Community

Discussion on: dotenv but 3 lines of ruby

Collapse
 
stympy profile image
Ben Curtis

This is great! Here's an alternative to the 2 lines of code in the loop:

    ENV.update l.chomp.scan(/^(?:export )?(\w+)\s*=\s*(?:['"])?([^'"]+)/i).to_h

This change also handles .env files that have lines in the format of export FOO=bar.

Collapse
 
kwstannard profile image
Kelly Stannard

Nice!

I will personally leave out the export stuff because I don't actually need to be compatible with dotenv, but it would be nice for people who do.

Collapse
 
kwstannard profile image
Kelly Stannard

How about 3 lines or fairly readable ruby?

#!/usr/bin/env ruby

regexp = /^(\w+)=['"]?(.+?)['"]?$/

ENV.update File.read(".env").scan(regexp).to_h if File.exist?(".env")
exec(ENV, *ARGV)