Created
June 3, 2011 02:24
-
-
Save trshafer/1005739 to your computer and use it in GitHub Desktop.
class_accesssor
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
class Dude < Person | |
awesomeness "SUPER AWESOME" | |
intellect 80 | |
end | |
# Dude.awesomeness #=> "SUPER AWESOME" | |
# Dude.new.intellect #=> 80 |
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
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