Created
March 20, 2014 04:18
-
-
Save danimal141/9657177 to your computer and use it in GitHub Desktop.
Define accessor sample
This file contains hidden or 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 Class | |
def define_accessor *args | |
args.each do |prop| | |
self.class_eval do | |
define_method prop do | |
return instance_variable_get("@#{prop}") | |
end | |
define_method "#{prop}=" do |val| | |
return instance_variable_set("@#{prop}", val) | |
end | |
end | |
end | |
end | |
end | |
class Hoge | |
define_accessor :name | |
end | |
hoge = Hoge.new | |
hoge.name = 'hoge' | |
p hoge.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment