vagrantfile - ruby - check if either of two variables exist -
i need check if either of 2 environment variables env_http_proxy or http_proxy set. if either set, assign value local variable. if neither of them exist, local variable should set nil.
http_proxy = defined?(env['env_http_proxy']) ? env['env_http_proxy'] : defined?(env['http_proxy']) ? env['http_proxy'] : nil whats wrong this? seem work first variable not second variable. (ps: if matters, trying use in vagrantfile)
also, can please explain above syntax. ruby noob. quick search showed defined?() function. not above extended syntax.
t = env['env_http_proxy'] || env['http_proxy'] as general rule, don't use defined? anything. it's metaprogramming primitive intended implementing called magic1..
the e1 ? e2 : e3 operator works c, testing expression e1 , taking value of e2 or e3. it's not used in ruby in other languages.
since env hash-like object, return nil when no key exists, , 1 of zillion awesome features of ruby way || operator returns value of true expression.
1. magic: noun, see: ruby on rails.
Comments
Post a Comment