Skip to content

Instantly share code, notes, and snippets.

@danimal141
Created March 20, 2014 04:18
Show Gist options
  • Save danimal141/9657177 to your computer and use it in GitHub Desktop.
Save danimal141/9657177 to your computer and use it in GitHub Desktop.
Define accessor sample
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