Skip to content

Instantly share code, notes, and snippets.

@jpignata
Created January 9, 2010 02:35
Show Gist options
  • Save jpignata/272675 to your computer and use it in GitHub Desktop.
Save jpignata/272675 to your computer and use it in GitHub Desktop.
class Object
def attribute(attribute, &block)
attribute, default_value = attribute.to_a.first if attribute.is_a?(Hash)
define_method(:"#{attribute}") do
if instance_variables.include?("@#{attribute}")
instance_variable_get("@#{attribute}")
elsif block_given?
instance_eval(&block)
else
default_value
end
end
define_method(:"#{attribute}?") { !!send(attribute.to_sym) }
define_method(:"#{attribute}=") { |args| instance_variable_set("@#{attribute}", args) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment