Skip to content

Instantly share code, notes, and snippets.

@adambray
Created July 26, 2013 23:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adambray/6092991 to your computer and use it in GitHub Desktop.
Save adambray/6092991 to your computer and use it in GitHub Desktop.
Sample ruby class with dynamic attributes in a hash.
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