Logo

Programming-Idioms

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".
Implementation
Ruby

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Ruby implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
  call get_environment_variable ("FOO", length=n, status=st)
  if (st /= 0) then
    foo = "none"
  else
    allocate (character(len=n) :: foo)
    call get_environment_variable ("FOO", value=foo)
  end if 
use 5.010;
my $foo = $ENV{FOO} // 'none';
import "os"
foo, ok := os.LookupEnv("FOO")
if !ok {
	foo = "none"
}
#include <stdlib.h>
const char * foo = getenv("FOO");
if (foo == NULL) foo = "none";
uses sysutils;
var
  foo: string;
begin
  foo := GetEnvironmentVariable('FOO');
  if (foo = '') then foo := 'none';
end.
import os
try:
    foo = os.environ['FOO']
except KeyError:
    foo = "none"
from os import getenv
foo = getenv('FOO', 'none')
imports System
Dim _foo as String
Try
	foo = Environment.GetEnvironmentVariable("FOO")
Catch ex as Exception
	foo = "none"
End Try
import "os"
foo := os.Getenv("FOO")
if foo == "" {
	foo = "none"
}
Foo = os:getenv("FOO","none").
string foo = Environment.GetEnvironmentVariable("FOO");
if (string.IsNullOrEmpty(foo)) foo = "none";
use std::env;
let foo = match env::var("FOO") {
    Ok(val) => val,
    Err(_e) => "none".to_string(),
};
#include <cstdlib>
#include <string>
const char* tmp = std::getenv("FOO");
std::string foo = tmp ? std::string(tmp) : "none";
const foo = process.env["FOO"] || "none";
foo = System.get_env("FOO", "none")
use std::env;
let foo = env::var("FOO").unwrap_or("none".to_string());
def foo = System.getenv('FOO') ?: 'none'
use std::env;
let foo = match env::var("FOO") {
    Ok(val) => val,
    Err(_e) => "none".to_string(),
};
String foo = System.getenv("foo");
if (foo == null) {
	foo = "none";
}
import os
foo = os.environ.get('FOO', 'none')
$foo = getenv("FOO") ?: "none";
use std::env;
if let Ok(tnt_root) = env::var("TNT_ROOT") {
     //
}
import Control.Exception (IOException, catch)
import System.Environment (getEnv)
do
  foo <- catch (getEnv "FOO") (const $ pure "none" :: IOException -> IO String)
  -- do something with foo
const foo = process.env.FOO ?? 'none'
(def foo
  (or (System/getenv "FOO") "none"))
import std.process : environment;
string foo = environment.get("FOO", "none");
foo = IIf(Environ("FOO") = "", "none", Environ("FOO"))
import 'dart:io';
var foo = Platform.environment["FOO"] ?? "none";
with Ada.Environment_Variables;
Foo : constant String :=
      Ada.Environment_Variables.Value (Name    => "FOO",
                                       Default => "none");
val foo = System.getenv("FOO") ?: "none"
| foo |
foo := CEnvironment getUserEnvironment at: 'FOO' ifAbsent: [ 'none' ].
foo = os.getenv("FOO") or "none"
val foo = sys.env.getOrElse("FOO", "none")