Skip to content

Instantly share code, notes, and snippets.

@trshafer
Created June 3, 2011 02:24
Show Gist options
  • Save trshafer/1005739 to your computer and use it in GitHub Desktop.
Save trshafer/1005739 to your computer and use it in GitHub Desktop.
class_accesssor
class Dude < Person
awesomeness "SUPER AWESOME"
intellect 80
end
# Dude.awesomeness #=> "SUPER AWESOME"
# Dude.new.intellect #=> 80
class Person
class << self
def class_accessor *method_names
method_names.each do |method|
(class << self; self; end).class_eval do
define_method method do |*args|
args.first.nil? ? instance_variable_get("@#{method}") : instance_variable_set("@#{method}", args.first)
end
end
define_method method do
self.class.instance_variable_get("@#{method}")
end
end
end
end
class_accessor :awesomeness, :intellect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment