Created
June 3, 2019 15:46
-
-
Save normancapule/f5e82ab52fc7d6779871c841683f9c3e to your computer and use it in GitHub Desktop.
Playing around with method missing
This file contains hidden or 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 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