-
-
Save leejarvis/5d8514287ed5e757b967e5bd4917774e to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
module ClassInstanceVariables | |
def class_instance_variable(name, value = nil, private: true) | |
(class << self; self; end).instance_eval { | |
attr_accessor name | |
private name if private | |
} | |
public_send("#{name}=", value) | |
attr_writer name | |
define_method(name) { value } | |
private name if private | |
end | |
end | |
class Foo | |
extend ClassInstanceVariables | |
class_instance_variable :foo | |
class_instance_variable :baz, 87, private: false | |
end | |
raise unless Foo.send(:foo) == nil | |
raise unless Foo.new.send(:foo) == nil | |
raise unless Foo.baz == 87 | |
raise unless Foo.new.baz == 87 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment