Skip to content

Instantly share code, notes, and snippets.

@lukeledet
Created December 14, 2015 20:57
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 lukeledet/fb7d044ef645fae00a2e to your computer and use it in GitHub Desktop.
Save lukeledet/fb7d044ef645fae00a2e to your computer and use it in GitHub Desktop.
Using hash keys to create instance variables
# Test double for a request
class RequestDouble
attr_accessor :env
def initialize hash
@env = {}
# Turn each hash entry into an instance variable
hash.each do |key, value|
instance_variable_set "@#{key}", value
self.class.class_eval do
attr_accessor key
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment