Created
December 14, 2015 20:57
-
-
Save lukeledet/fb7d044ef645fae00a2e to your computer and use it in GitHub Desktop.
Using hash keys to create instance variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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