Skip to content

Instantly share code, notes, and snippets.

@Nowaker
Created March 1, 2012 03:15
Show Gist options
  • Save Nowaker/1946964 to your computer and use it in GitHub Desktop.
Save Nowaker/1946964 to your computer and use it in GitHub Desktop.
attr_reader and attr_writer on class instance
class Magic
def property types, method_name, property_name = nil
property_name = method_name if property_name.nil?
# reader
self.class.send :define_method, method_name do
value = read_value property_name
raise unless types.any? do |type|
value.is_a? type
end
value
end
# writer
self.class.send :define_method, method_name + '=' do |value|
raise unless types.any? do |type|
value.is_a? type
end
change_value property_name, value
end
end
property [String, Integer], :online_mode
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment