Skip to content

Instantly share code, notes, and snippets.

@kyanny
Last active December 14, 2015 15:19
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 kyanny/26b5cb9b3e06b982be54 to your computer and use it in GitHub Desktop.
Save kyanny/26b5cb9b3e06b982be54 to your computer and use it in GitHub Desktop.
require 'delegate'
require 'ostruct'
class MyProxyClass < SimpleDelegator
def initialize(env)
super(OpenStruct.new(env))
end
def method_missing(method, *args)
super
rescue NoMethodError
super(method.upcase, *args)
rescue NoMethodError
nil
end
end
env_proxy = MyProxyClass.new(ENV)
p env_proxy.USER
p env_proxy.user
p env_proxy.unknown_environment_variable
@kyanny
Copy link
Author

kyanny commented Mar 7, 2013

$ ruby test_delegate.rb
"usr0600121"
"usr0600121"
test_delegate.rb:12:in `rescue in method_missing': undefined method `UNKNOWN_ENVIRONMENT_VARIABLE' for #<MyProxyClass:0x007fe2120247b0> (NoMethodError)
        from test_delegate.rb:10:in `method_missing'
        from test_delegate.rb:19:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment