Skip to content

Instantly share code, notes, and snippets.

@kisai
Created August 23, 2014 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kisai/e4b22df97c9ad9125b86 to your computer and use it in GitHub Desktop.
Save kisai/e4b22df97c9ad9125b86 to your computer and use it in GitHub Desktop.
My attr_accesor.rb
class Class
#firstly, the * decoration on the parameter variable
#indicates that the parameters should come in as an array
#of whatever was sent
def my_attr_accessor(*args)
#We simply iterate through each passed in argument...
args.each do |arg|
#Here's the getter
self.class_eval("def #{arg};@#{arg};end")
#Here's the setter
self.class_eval("def #{arg}=(val);@#{arg}=val;end")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment