Skip to content

Instantly share code, notes, and snippets.

@normancapule
Created June 3, 2019 15:46
Show Gist options
  • Save normancapule/f5e82ab52fc7d6779871c841683f9c3e to your computer and use it in GitHub Desktop.
Save normancapule/f5e82ab52fc7d6779871c841683f9c3e to your computer and use it in GitHub Desktop.
Playing around with method missing
module Powers
def wattr_accessor(*attrs)
wattr_reader(*attrs)
wattr_writer(*attrs)
end
def wattr_reader(*attrs)
attrs.each do |attr|
define_method "#{attr}" do
instance_variable_get(:"@#{attr}")
end
end
end
def wattr_writer(*attrs)
attrs.each do |attr|
define_method "#{attr}=" do |value|
instance_variable_set(:"@#{attr}", value)
end
end
end
end
class Wat
extend Powers
wattr_accessor :wat
wattr_reader :lala
wattr_writer :lala
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment