Created
November 17, 2009 05:07
-
-
Save santosh79/236660 to your computer and use it in GitHub Desktop.
Accessors with class_eval
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
module Accessor | |
def my_attr_accessor(*names) | |
names.each do |name| | |
class_eval %{ | |
def #{name} | |
@#{name} | |
end | |
def #{name}=(val) | |
@#{name} = val | |
end | |
} | |
end | |
end | |
end | |
class Foo | |
extend Accessor | |
my_attr_accessor :val, :bar | |
end | |
f = Foo.new | |
f.val = 1 | |
f.bar = 2 | |
puts "val: #{f.val}" | |
puts "bar: #{f.bar}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment