Skip to content

Instantly share code, notes, and snippets.

@Olefine
Created June 26, 2013 13:03
Show Gist options
  • Select an option

  • Save Olefine/5867194 to your computer and use it in GitHub Desktop.

Select an option

Save Olefine/5867194 to your computer and use it in GitHub Desktop.
2 ways define method
class Module
def access(*args)
args.each do |arg|
instance_var = ("@" + arg.to_s)
define_method(arg.to_s + '=') {|val| instance_variable_set(instance_var, val)}
define_method(arg) { instance_variable_get(instance_var)}
end
end
def attr_access(*args)
args.each do |arg|
module_eval "def #{arg}; @#{arg}; end;"
module_eval "def #{arg}=(val); @#{arg} = val; end;"
end
end
end
class Person
attr_access :name, :age, :long
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment