Created
July 26, 2013 23:47
-
-
Save adambray/6092991 to your computer and use it in GitHub Desktop.
Sample ruby class with dynamic attributes in a hash.
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
module SN | |
class Incident | |
def initialize(attributes = {}) | |
@attributes = attributes | |
end | |
def method_missing(method, args = nil) | |
method_name = method.to_s | |
if match = method_name.match(/(.*)=/) # writer method | |
attribute = match[1] | |
@attributes[attribute] = args | |
else # reader method | |
@attributes[method_name] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment